home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNUC / UTIL-41S.LZH / util-41 / ld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  174.9 KB  |  6,427 lines

  1. /* Linker `ld' for GNU
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Richard Stallman with some help from Eric Albert.
  19.    Set, indirect, and warning symbol features added by Randy Smith.  */
  20.  
  21. #ifdef BYTE_SWAP
  22.  
  23. #define SWAP4(y) (((unsigned)(y)>>24) + (((unsigned)(y)>>8)&0xff00) + \
  24.          (((unsigned)(y)<<8)&0xff0000) + ((unsigned)(y)<<24)) 
  25. #define SWAP2(y) ((((unsigned)(y)&0xff00)>>8) + (((unsigned)(y)&0x00ff)<<8))
  26.  
  27. #endif /* BYTE_SWAP */
  28.  
  29.  
  30. #ifdef CROSSATARI
  31. #include "cross-inc/gnu-out.h"
  32. #include "cross-inc/gnu-ar.h"
  33. #include "cross-inc/st-out.h"
  34. #include <stdio.h>
  35. #include <sys/types.h>
  36. #include <string.h>
  37. #include <sys/stat.h>
  38. #include <sys/file.h>
  39. #include <ctype.h>
  40. #ifndef __GNUC__
  41. #include <time.h>
  42. #endif
  43. #include "cross-inc/stab.h"
  44. #else
  45. #include <gnu-out.h>
  46. #include <st-out.h>
  47. #include <gnu-ar.h>
  48. #include <stdio.h>
  49. #include <types.h>
  50. #include <string.h>
  51. #include <stat.h>
  52. #include <file.h>
  53. #include <ctype.h>
  54. #include <unistd.h>
  55. #include <stab.h>
  56. #endif        /* atarist */
  57.  
  58. #define CORE_ADDR unsigned long    /* For symseg.h */
  59. #include "symseg.h"
  60.  
  61. /* If compiled with GNU C, use the built-in alloca */
  62. #ifdef __GNUC__
  63. #define alloca(X) __builtin_alloca(X)
  64. #endif
  65.  
  66.  
  67. #ifdef USG
  68. #include <string.h>
  69. #else
  70. #include <strings.h>
  71. #endif
  72.  
  73. #if __STDC__
  74. #  include <stdarg.h>
  75. #else
  76. #  include <varargs.h>
  77. #endif
  78.  
  79. /* Determine whether we should attempt to handle (minimally)
  80.    N_BINCL and N_EINCL.  */
  81.  
  82. #if defined (__GNU_STAB__) || defined (N_BINCL)
  83. #define HAVE_SUN_STABS
  84. #endif
  85.  
  86. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  87. /* stuff used when emitting reloc info here */
  88.  
  89. #define RBUF_SIZE 1024
  90.  
  91. #ifdef CROSSATARI
  92. unsigned char rbuf[RBUF_SIZE];
  93. #else
  94. char rbuf[RBUF_SIZE];
  95. #endif
  96. int rbuf_size = 0;        /* how many bytes are in there now */
  97. long rbuf_last_pc = -1;        /* last rel pc; -1 means none yet */
  98.  
  99. #ifdef atarist
  100. long _stksize = 32768L;
  101. #endif
  102. #endif
  103.  
  104. #ifdef min
  105. #undef min
  106. #endif
  107. #define min(a,b) ((a) < (b) ? (a) : (b))
  108.  
  109. /* Macro to control the number of undefined references printed */
  110. #define MAX_UREFS_PRINTED    10
  111.  
  112. /* Size of a page; obtained from the operating system.  */
  113.  
  114. int page_size;
  115.  
  116. /* Name this program was invoked by.  */
  117. extern void dump_version(char *prog);
  118. char *progname = "ld";
  119.  
  120.  
  121. /* System dependencies */
  122.  
  123. /* Define this if names etext, edata and end should not start with `_'.  */
  124. /* #define nounderscore 1 */
  125.  
  126. /* Define NON_NATIVE if using BSD or pseudo-BSD file format on a system
  127.    whose native format is different.  */
  128. /* #define NON_NATIVE */
  129.  
  130. /* Define this to specify the default executable format.  */
  131.  
  132. #ifndef CROSSHPUX
  133. #ifdef hpux
  134. #define DEFAULT_MAGIC NMAGIC  /* hpux bugs screw ZMAGIC */
  135. #endif
  136. #endif
  137.  
  138. #ifndef DEFAULT_MAGIC
  139. #define DEFAULT_MAGIC ZMAGIC
  140. #endif
  141.  
  142. /* Ordinary 4.3bsd lacks these macros in a.out.h.  */
  143.  
  144. #ifndef N_TXTADDR
  145. #ifdef vax
  146. #define N_TXTADDR(X) 0
  147. #endif
  148. #ifdef is68k
  149. #define N_TXTADDR(x)  (sizeof (struct exec))
  150. #endif
  151. #endif
  152.  
  153. #ifndef N_DATADDR
  154. #ifdef vax
  155. #define N_DATADDR(x) \
  156.     (((x).a_magic==OMAGIC)? (N_TXTADDR(x)+(x).a_text) \
  157.     : (page_size+((N_TXTADDR(x)+(x).a_text-1) & ~(page_size-1))))
  158. #endif
  159. #ifdef is68k
  160. #define SEGMENT_SIZE 0x20000
  161. #define N_DATADDR(x) \
  162.     (((x).a_magic==Omagic)? (N_TXTADDR(x)+(x).a_text) \
  163.      : (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))))
  164. #endif
  165. #endif
  166.  
  167. #ifndef CROSSHPUX
  168. #ifdef hpux
  169. #define getpagesize() EXEC_PAGESIZE
  170. #endif
  171. #endif
  172.  
  173. #if (defined(atarist) || defined(atariminix) || defined(CROSSATARI))
  174. /* kludgerama, not really used */
  175. #define getpagesize() 2
  176. #endif
  177.  
  178. /* Define how to initialize system-dependent header fields.  */
  179. #ifndef CROSSATARI
  180. #ifdef sun
  181. #ifdef sparc
  182. #define INITIALIZE_HEADER \
  183.   {outheader.a_machtype = M_SPARC; outheader.a_toolversion = 1;}
  184. #endif
  185. #if defined(mc68010) || defined(m68010) || (TARGET == SUN2)
  186. #define INITIALIZE_HEADER outheader.a_machtype = M_68010
  187. #endif
  188. #ifndef INITIALIZE_HEADER
  189. #define INITIALIZE_HEADER outheader.a_machtype = M_68020
  190. #endif
  191. #endif
  192. #ifdef is68k
  193. #ifdef M_68020
  194. /* ISI rel 4.0D doesn't use it, and rel 3.05 doesn't have an
  195.    a_machtype field and so won't recognize the magic number.  To keep
  196.    binary compatibility for now, just ignore it */
  197. #define INITIALIZE_HEADER outheader.a_machtype = 0;
  198. #endif
  199. #endif
  200. #ifndef CROSSHPUX
  201. #ifdef hpux
  202. #define INITIALIZE_HEADER outheader.a_machtype = HP9000S200_ID
  203. #endif
  204. #endif
  205. #ifdef i386
  206. #define INITIALIZE_HEADER outheader.a_machtype = M_386
  207. #endif
  208. #endif /* CROSSATARI */
  209.  
  210. #ifdef is68k
  211. /* This enables code to take care of an ugly hack in the ISI OS.
  212.    If a symbol beings with _$, then the object file is included only
  213.    if the rest of the symbol name has been referenced. */
  214. #define DOLLAR_KLUDGE
  215. #endif
  216.  
  217. /*
  218.  * Alloca include.
  219.  */
  220. #if defined(sun) && defined(sparc)
  221. #ifdef alloca
  222. #undef alloca
  223. #endif
  224. #include <alloca.h>
  225. #endif
  226.  
  227. #ifdef mips        /* TUHH CF */
  228. #include <alloca.h>
  229. #endif
  230.  
  231. #ifndef L_SET
  232. #define L_SET 0
  233. #endif
  234.  
  235. /*
  236.  * Ok.  Following are the relocation information macros.  If your
  237.  * system should not be able to use the default set (below), you must
  238.  * define the following:
  239.  
  240.  *   relocation_info: This must be typedef'd (or #define'd to the type
  241.  * of structure that is stored in the relocation info section of your
  242.  * a.out files.  Often this is defined in the a.out.h for your system.
  243.  *
  244.  *   RELOC_ADDRESS (rval): Offset into the current section of the
  245.  * <whatever> to be relocated.  *Must be an lvalue*.
  246.  *
  247.  *   RELOC_EXTERN_P (rval):  Is this relocation entry based on an
  248.  * external symbol (1), or was it fully resolved upon entering the
  249.  * loader (0) in which case some combination of the value in memory
  250.  * (if RELOC_MEMORY_ADD_P) and the extra (if RELOC_ADD_EXTRA) contains
  251.  * what the value of the relocation actually was.  *Must be an lvalue*.
  252.  *
  253.  *   RELOC_TYPE (rval): If this entry was fully resolved upon
  254.  * entering the loader, what type should it be relocated as?
  255.  *
  256.  *   RELOC_SYMBOL (rval): If this entry was not fully resolved upon
  257.  * entering the loader, what is the index of it's symbol in the symbol
  258.  * table?  *Must be a lvalue*.
  259.  *
  260.  *   RELOC_MEMORY_ADD_P (rval): This should return true if the final
  261.  * relocation value output here should be added to memory, or if the
  262.  * section of memory described should simply be set to the relocation
  263.  * value.
  264.  *
  265.  *   RELOC_ADD_EXTRA (rval): (Optional) This macro, if defined, gives
  266.  * an extra value to be added to the relocation value based on the
  267.  * individual relocation entry.
  268.  *
  269.  *   RELOC_PCREL_P (rval): True if the relocation value described is
  270.  * pc relative.
  271.  *
  272.  *   RELOC_VALUE_RIGHTSHIFT (rval): Number of bits right to shift the
  273.  * final relocation value before putting it where it belongs.
  274.  *
  275.  *   RELOC_TARGET_SIZE (rval): log to the base 2 of the number of
  276.  * bytes of size this relocation entry describes; 1 byte == 0; 2 bytes
  277.  * == 1; 4 bytes == 2, and etc.  This is somewhat redundant (we could
  278.  * do everything in terms of the bit operators below), but having this
  279.  * macro could end up producing better code on machines without fancy
  280.  * bit twiddling.  Also, it's easier to understand/code big/little
  281.  * endian distinctions with this macro.
  282.  *
  283.  *   RELOC_TARGET_BITPOS (rval): The starting bit position within the
  284.  * object described in RELOC_TARGET_SIZE in which the relocation value
  285.  * will go.
  286.  *
  287.  *   RELOC_TARGET_BITSIZE (rval): How many bits are to be replaced
  288.  * with the bits of the relocation value.  It may be assumed by the
  289.  * code that the relocation value will fit into this many bits.  This
  290.  * may be larger than RELOC_TARGET_SIZE if such be useful.
  291.  *
  292.  *
  293.  *        Things I haven't implemented
  294.  *        ----------------------------
  295.  *
  296.  *    Values for RELOC_TARGET_SIZE other than 0, 1, or 2.
  297.  *
  298.  *    Pc relative relocation for External references.
  299.  *
  300.  *
  301.  */
  302.  
  303. #if defined(sun) && defined(sparc) && !defined(CROSSATARI)
  304. /* Sparc (Sun 4) macros */
  305. #undef relocation_info
  306. #define relocation_info                    reloc_info_sparc
  307. #define RELOC_ADDRESS(r)        ((r)->r_address)                 
  308. #define RELOC_EXTERN_P(r)               ((r)->r_extern)      
  309. #define RELOC_TYPE(r)                   ((r)->r_index)  
  310. #define RELOC_SYMBOL(r)                 ((r)->r_index)   
  311. #define RELOC_MEMORY_ADD_P(r)           0                          
  312. #define RELOC_ADD_EXTRA(r)              ((r)->r_addend)       
  313. #define RELOC_PCREL_P(r)             \
  314.         ((r)->r_type >= RELOC_DISP8 && (r)->r_type <= RELOC_WDISP22)
  315. #define RELOC_VALUE_RIGHTSHIFT(r)       (reloc_target_rightshift[(r)->r_type])
  316. #define RELOC_TARGET_SIZE(r)            (reloc_target_size[(r)->r_type])
  317. #define RELOC_TARGET_BITPOS(r)          0
  318. #define RELOC_TARGET_BITSIZE(r)         (reloc_target_bitsize[(r)->r_type])
  319.  
  320. /* Note that these are very dependent on the order of the enums in
  321.    enum reloc_type (in a.out.h); if they change the following must be
  322.    changed */
  323. /* Also note that the last few may be incorrect; I have no information */
  324. static int reloc_target_rightshift[] = {
  325.   0, 0, 0, 0, 0, 0, 2, 2, 10, 0, 0, 0, 0, 0, 0,
  326. };
  327. static int reloc_target_size[] = {
  328.   0, 1, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  329. };
  330. static int reloc_target_bitsize[] = {
  331.   8, 16, 32, 8, 16, 32, 30, 22, 22, 22, 13, 10, 32, 32, 16,
  332. };
  333. #endif
  334.  
  335. /* Default macros */
  336. #ifndef RELOC_ADDRESS
  337. #define RELOC_ADDRESS(r)        ((r)->r_address)
  338. #define RELOC_EXTERN_P(r)        ((r)->r_extern)
  339. #define RELOC_TYPE(r)        ((r)->r_symbolnum)
  340. #define RELOC_SYMBOL(r)        ((r)->r_symbolnum)
  341. #define RELOC_MEMORY_ADD_P(r)    1
  342. /* #define RELOC_ADD_EXTRA(r)        0     */ /* Don't need to define */
  343. #define RELOC_PCREL_P(r)        ((r)->r_pcrel)
  344. #define RELOC_VALUE_RIGHTSHIFT(r)    0
  345. #define RELOC_TARGET_SIZE(r)        ((r)->r_length)
  346. #define RELOC_TARGET_BITPOS(r)    0
  347. #define RELOC_TARGET_BITSIZE(r)    32
  348. #endif
  349. #if defined(atarist) || defined(CROSSATARI)
  350. #define RELOC_ADD_EXTRA(r)    \
  351.     ((!RELOC_PCREL_P(r) && RELOC_TARGET_SIZE(r) < 2) \
  352.         ? BASE_OFFSET-text_size : 0)
  353. #endif
  354.  
  355. /* Special global symbol types understood by GNU LD.  */
  356.  
  357. /* The following type indicates the definition of a symbol as being
  358.    an indirect reference to another symbol.  The other symbol
  359.    appears as an undefined reference, immediately following this symbol.
  360.  
  361.    Indirection is asymmetrical.  The other symbol's value will be used
  362.    to satisfy requests for the indirect symbol, but not vice versa.
  363.    If the other symbol does not have a definition, libraries will
  364.    be searched to find a definition.  */
  365. #ifndef N_INDR
  366. #define N_INDR 0xa
  367. #endif
  368.  
  369. /* The following symbols refer to set elements.  These are expected
  370.    only in input to the loader; they should not appear in loader
  371.    output (unless relocatable output is requested).  To be recognized
  372.    by the loader, the input symbols must have their N_EXT bit set.
  373.    All the N_SET[ATDB] symbols with the same name form one set.  The
  374.    loader collects all of these elements at load time and outputs a
  375.    vector for each name.
  376.    Space (an array of 32 bit words) is allocated for the set in the
  377.    data section, and the n_value field of each set element value is
  378.    stored into one word of the array.
  379.    The first word of the array is the length of the set (number of
  380.    elements).  The last word of the vector is set to zero for possible
  381.    use by incremental loaders.  The array is ordered by the linkage
  382.    order; the first symbols which the linker encounters will be first
  383.    in the array.
  384.  
  385.    In C syntax this looks like:
  386.  
  387.     struct set_vector {
  388.       unsigned int length;
  389.       unsigned int vector[length];
  390.       unsigned int always_zero;
  391.     };
  392.  
  393.    Before being placed into the array, each element is relocated
  394.    according to its type.  This allows the loader to create an array
  395.    of pointers to objects automatically.  N_SETA type symbols will not
  396.    be relocated.
  397.  
  398.    The address of the set is made into an N_SETV symbol
  399.    whose name is the same as the name of the set.
  400.    This symbol acts like a N_TEXT global symbol
  401.    in that it can satisfy undefined external references.
  402.  
  403.    For the purposes of determining whether or not to load in a library
  404.    file, set element definitions are not considered "real
  405.    definitions"; they will not cause the loading of a library
  406.    member.
  407.  
  408.    If relocatable output is requested, none of this processing is
  409.    done.  The symbols are simply relocated and passed through to the
  410.    output file.
  411.  
  412.    So, for example, the following three lines of assembler code
  413.    (whether in one file or scattered between several different ones)
  414.    will produce a three element vector (total length is five words;
  415.    see above), referenced by the symbol "_xyzzy", which will have the
  416.    addresses of the routines _init1, _init2, and _init3.
  417.  
  418.    *NOTE*: If symbolic addresses are used in the n_value field of the
  419.    defining .stabs, those symbols must be defined in the same file as
  420.    that containing the .stabs.
  421.  
  422.     .stabs "_xyzzy",23,0,0,_init1
  423.     .stabs "_xyzzy",23,0,0,_init2
  424.     .stabs "_xyzzy",23,0,0,_init3
  425.  
  426.    Note that (23 == (N_SETT | N_EXT)).  */
  427. #ifndef N_SETA
  428. #define    N_SETA    0x14        /* Absolute set element symbol */
  429. #endif                /* This is input to LD, in a .o file.  */
  430.  
  431. #ifndef N_SETT
  432. #define    N_SETT    0x16        /* Text set element symbol */
  433. #endif                /* This is input to LD, in a .o file.  */
  434.  
  435. #ifndef N_SETD
  436. #define    N_SETD    0x18        /* Data set element symbol */
  437. #endif                /* This is input to LD, in a .o file.  */
  438.  
  439. #ifndef N_SETB
  440. #define    N_SETB    0x1A        /* Bss set element symbol */
  441. #endif                /* This is input to LD, in a .o file.  */
  442.  
  443. /* Macros dealing with the set element symbols defined in a.out.h */
  444. #define    SET_ELEMENT_P(x)    ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
  445. #define TYPE_OF_SET_ELEMENT(x)    ((x)-N_SETA+N_ABS)
  446.  
  447. #ifndef N_SETV
  448. #define N_SETV    0x1C        /* Pointer to set vector in text area.  */
  449. #endif                /* This is output from LD.  */
  450.  
  451. /* If a this type of symbol is encountered, its name is a warning
  452.    message to print each time the symbol referenced by the next symbol
  453.    table entry is referenced.
  454.  
  455.    This feature may be used to allow backwards compatibility with
  456.    certain functions (eg. gets) but to discourage programmers from
  457.    their use.
  458.  
  459.    So if, for example, you wanted to have ld print a warning whenever
  460.    the function "gets" was used in their C program, you would add the
  461.    following to the assembler file in which gets is defined:
  462.  
  463.     .stabs "Obsolete function \"gets\" referenced",30,0,0,0
  464.     .stabs "_gets",1,0,0,0
  465.  
  466.    These .stabs do not necessarily have to be in the same file as the
  467.    gets function, they simply must exist somewhere in the compilation.  */
  468. #ifndef N_WARNING
  469. #define N_WARNING 0x1E        /* Warning message to print if file included */
  470. #endif                /* This is input to ld */
  471.  
  472. #ifndef __GNU_STAB__
  473.  
  474. /* Line number for the data section.  This is to be used to describe
  475.    the source location of a variable declaration.  */
  476. #ifndef N_DSLINE
  477. #define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
  478. #endif
  479.  
  480. /* Line number for the bss section.  This is to be used to describe
  481.    the source location of a variable declaration.  */
  482. #ifndef N_BSLINE
  483. #define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
  484. #endif
  485.  
  486. #endif /* not __GNU_STAB__ */
  487.  
  488. /* Symbol table */
  489.  
  490. /* Global symbol data is recorded in these structures,
  491.    one for each global symbol.
  492.    They are found via hashing in 'symtab', which points to a vector of buckets.
  493.    Each bucket is a chain of these structures through the link field.  */
  494.  
  495. typedef
  496.   struct glosym
  497.     {
  498.       /* Pointer to next symbol in this symbol's hash bucket.  */
  499.       struct glosym *link;
  500.       /* Name of this symbol.  */
  501.       char *name;
  502.       /* Value of this symbol as a global symbol.  */
  503.       long value;
  504.       /* Chain of external 'nlist's in files for this symbol, both defs
  505.      and refs.  */
  506.       struct nlist *refs;
  507.       /* Any warning message that might be associated with this symbol
  508.          from an N_WARNING symbol encountered. */
  509.       char *warning;
  510.       /* Nonzero means definitions of this symbol as common have been seen,
  511.      and the value here is the largest size specified by any of them.  */
  512.       int max_common_size;
  513.       /* For relocatable_output, records the index of this global sym in the
  514.      symbol table to be written, with the first global sym given index 0.*/
  515.       int def_count;
  516.       /* Nonzero means a definition of this global symbol is known to exist.
  517.      Library members should not be loaded on its account.  */
  518.       char defined;
  519.       /* Nonzero means a reference to this global symbol has been seen
  520.      in a file that is surely being loaded.
  521.      A value higher than 1 is the n_type code for the symbol's
  522.      definition.  */
  523.       char referenced;
  524.       /* A count of the number of undefined references printed for a
  525.      specific symbol.  If a symbol is unresolved at the end of
  526.      digest_symbols (and the loading run is supposed to produce
  527.      relocatable output) do_file_warnings keeps track of how many
  528.      unresolved reference error messages have been printed for
  529.      each symbol here.  When the number hits MAX_UREFS_PRINTED,
  530.      messages stop. */
  531.       unsigned char undef_refs;
  532.       /* Nonzero means print a message at all refs or defs of this symbol */
  533.       char trace;
  534.     }
  535.   symbol;
  536.  
  537. /* Demangler for C++. */
  538. extern char *cplus_demangle ();
  539.  
  540. /* Demangler function to use. */
  541. char *(*demangler)() = cplus_demangle;
  542.  
  543. /* Number of buckets in symbol hash table */
  544. #define    TABSIZE    1009
  545.  
  546. /* The symbol hash table: a vector of TABSIZE pointers to struct glosym. */
  547. symbol *symtab[TABSIZE];
  548.  
  549. /* Number of symbols in symbol hash table. */
  550. int num_hash_tab_syms = 0;
  551.  
  552. /* Count the number of nlist entries that are for local symbols.
  553.    This count and the three following counts
  554.    are incremented as as symbols are entered in the symbol table.  */
  555. int local_sym_count;
  556.  
  557. /* Count number of nlist entries that are for local symbols
  558.    whose names don't start with L. */
  559. int non_L_local_sym_count;
  560.  
  561. /* Count the number of nlist entries for debugger info.  */
  562. int debugger_sym_count;
  563.  
  564. /* Count the number of global symbols referenced and not defined.  */
  565. int undefined_global_sym_count;
  566.  
  567. /* Count the number of defined global symbols.
  568.    Each symbol is counted only once
  569.    regardless of how many different nlist entries refer to it,
  570.    since the output file will need only one nlist entry for it.
  571.    This count is computed by `digest_symbols';
  572.    it is undefined while symbols are being loaded. */
  573. int defined_global_sym_count;
  574.  
  575. /* Count the number of symbols defined through common declarations.
  576.    This count is kept in symdef_library, linear_library, and
  577.    enter_global_ref.  It is incremented when the defined flag is set
  578.    in a symbol because of a common definition, and decremented when
  579.    the symbol is defined "for real" (ie. by something besides a common
  580.    definition).  */
  581. int common_defined_global_count;
  582.  
  583. /* Count the number of set element type symbols and the number of
  584.    separate vectors which these symbols will fit into.  See the
  585.    GNU a.out.h for more info.
  586.    This count is computed by 'enter_file_symbols' */
  587. int set_symbol_count;
  588. int set_vector_count;
  589.  
  590. /* Count the number of definitions done indirectly (ie. done relative
  591.    to the value of some other symbol. */
  592. int global_indirect_count;
  593.  
  594. /* Count the number of warning symbols encountered. */
  595. int warning_count;
  596.  
  597. /* Total number of symbols to be written in the output file.
  598.    Computed by digest_symbols from the variables above.  */
  599. int nsyms;
  600.  
  601.  
  602. /* Nonzero means ptr to symbol entry for symbol to use as start addr.
  603.    -e sets this.  */
  604. symbol *entry_symbol;
  605.  
  606. symbol *edata_symbol;   /* the symbol _edata */
  607. symbol *etext_symbol;   /* the symbol _etext */
  608. symbol *end_symbol;    /* the symbol _end */
  609.  
  610. /* Each input file, and each library member ("subfile") being loaded,
  611.    has a `file_entry' structure for it.
  612.  
  613.    For files specified by command args, these are contained in the vector
  614.    which `file_table' points to.
  615.  
  616.    For library members, they are dynamically allocated,
  617.    and chained through the `chain' field.
  618.    The chain is found in the `subfiles' field of the `file_entry'.
  619.    The `file_entry' objects for the members have `superfile' fields pointing
  620.    to the one for the library.  */
  621.  
  622. struct file_entry {
  623.       /* Name of this file.  */
  624.   char *filename;
  625.       /* Name to use for the symbol giving address of text start */
  626.       /* Usually the same as filename, but for a file spec'd with -l
  627.          this is the -l switch itself rather than the filename.  */
  628.   char *local_sym_name;
  629.  
  630.       /* Describe the layout of the contents of the file */
  631.  
  632.       /* The file's a.out header.  */
  633.   struct exec header;
  634.       /* Offset in file of GDB symbol segment, or 0 if there is none.  */
  635.   int symseg_offset;
  636.  
  637.       /* Describe data from the file loaded into core */
  638.  
  639.       /* Symbol table of the file.  */
  640.   struct nlist *symbols;
  641.       /* Size in bytes of string table.  */
  642.   int string_size;
  643.       /* Pointer to the string table.
  644.          The string table is not kept in core all the time,
  645.          but when it is in core, its address is here.  */
  646.   char *strings;
  647.       /* Pointer to any warning specified in this file by an N_WARNING
  648.          type symbol */
  649.   char *warning;
  650.  
  651.       /* Next two used only if `relocatable_output' or if needed for */
  652.       /* output of undefined reference line numbers. */
  653.  
  654.       /* Text reloc info saved by `write_text' for `coptxtrel'.  */
  655.   struct relocation_info *textrel;
  656.       /* Data reloc info saved by `write_data' for `copdatrel'.  */
  657.   struct relocation_info *datarel;
  658.  
  659.       /* Relation of this file's segments to the output file */
  660.  
  661.       /* Start of this file's text seg in the output file core image.  */
  662.   int text_start_address;
  663.       /* Start of this file's data seg in the output file core image.  */
  664.   int data_start_address;
  665.       /* Start of this file's bss seg in the output file core image.  */
  666.   int bss_start_address;
  667.       /* Offset in bytes in the output file symbol table of the first
  668.          local symbol for this file.  Set by `write_file_symbols'.  */
  669.   int local_syms_offset;
  670.  
  671.       /* For library members only */
  672.  
  673.       /* For a library, points to chain of entries for the library members. */
  674.   struct file_entry *subfiles;
  675.       /* For a library member, offset of the member within the archive.
  676.          Zero for files that are not library members.  */
  677.   int starting_offset;
  678.       /* Size of contents of this file, if library member.  */
  679.   int total_size;
  680.       /* For library member, points to the library's own entry.  */
  681.   struct file_entry *superfile;
  682.       /* For library member, points to next entry for next member.  */
  683.   struct file_entry *chain;
  684.  
  685.       /* 1 if file is a library. */
  686.   char library_flag;
  687.  
  688.       /* 1 if file's header has been read into this structure.  */
  689.   char header_read_flag;
  690.  
  691.       /* 1 means search a set of directories for this file.  */
  692.   char search_dirs_flag;
  693.  
  694.       /* 1 means this is base file of incremental load.
  695.          Do not load this file's text or data.
  696.          Also default text_start to after this file's bss. */
  697.   char just_syms_flag;
  698. };
  699.  
  700. /* Vector of entries for input files specified by arguments.
  701.    These are all the input files except for members of specified libraries.  */
  702. struct file_entry *file_table;
  703.  
  704. /* Length of that vector.  */
  705. int number_of_files;
  706.  
  707. /* When loading the text and data, we can avoid doing a close
  708.    and another open between members of the same library.
  709.  
  710.    These two variables remember the file that is currently open.
  711.    Both are NULL if no file is open.
  712.  
  713.    See `each_file' and `file_close'.  */
  714.  
  715. struct file_entry *input_file;
  716. FILE *input_desc = NULL;
  717.  
  718. /* The name of the file to write; "a.out" by default.  */
  719.  
  720. char *output_filename;
  721.  
  722. /* Descriptor for writing that file with `mywrite'.  */
  723.  
  724. FILE *outdesc = NULL;
  725.  
  726. /* Header for that file (filled in by `write_header').  */
  727.  
  728. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  729. struct aexec outheader;
  730. #else
  731. struct exec outheader;
  732. #endif
  733.  
  734. #ifdef COFF_ENCAPSULATE
  735. struct coffheader coffheader;
  736. int need_coff_header;
  737. #endif
  738.  
  739. /* The following are computed by `digest_symbols'.  */
  740.  
  741. int text_size;        /* total size of text of all input files.  */
  742. int data_size;        /* total size of data of all input files.  */
  743. int bss_size;        /* total size of bss of all input files.  */
  744. int text_reloc_size;    /* total size of text relocation of all input files.  */
  745. int data_reloc_size;    /* total size of data relocation of all input */
  746.             /* files.  */
  747.  
  748. /* Specifications of start and length of the area reserved at the end
  749.    of the text segment for the set vectors.  Computed in 'digest_symbols' */
  750. int set_sect_start;
  751. int set_sect_size;
  752.  
  753. /* Pointer for in core storage for the above vectors, before they are
  754.    written. */
  755. unsigned long *set_vectors;
  756.  
  757. /* Amount of cleared space to leave between the text and data segments.  */
  758.  
  759. int text_pad;
  760.  
  761. /* Amount of bss segment to include as part of the data segment.  */
  762.  
  763. int data_pad;
  764.  
  765. /* Format of __.SYMDEF:
  766.    First, a longword containing the size of the 'symdef' data that follows.
  767.    Second, zero or more 'symdef' structures.
  768.    Third, a word containing the length of symbol name strings.
  769.    Fourth, zero or more symbol name strings (each followed by a zero).  */
  770.  
  771. struct symdef {
  772.   int symbol_name_string_index;
  773.   int library_member_offset;
  774. };
  775.  
  776. /* Record most of the command options.  */
  777.  
  778. /* Address we assume the text section will be loaded at.
  779.    We relocate symbols and text and data for this, but we do not
  780.    write any padding in the output file for it.  */
  781. int text_start;
  782.  
  783. /* Offset of default entry-pc within the text section.  */
  784. int entry_offset;
  785.  
  786. /* Address we decide the data section will be loaded at.  */
  787. int data_start;
  788.  
  789. /* `text-start' address is normally this much plus a page boundary.
  790.    This is not a user option; it is fixed for each system.  */
  791. int text_start_alignment;
  792.  
  793. /* Nonzero if -T was specified in the command line.
  794.    This prevents text_start from being set later to default values.  */
  795. int T_flag_specified;
  796.  
  797. /* Nonzero if -Tdata was specified in the command line.
  798.    This prevents data_start from being set later to default values.  */
  799. int Tdata_flag_specified;
  800.  
  801. /* Size to pad data section up to.
  802.    We simply increase the size of the data section, padding with zeros,
  803.    and reduce the size of the bss section to match.  */
  804. int specified_data_size;
  805.  
  806. /* Magic number to use for the output file, set by switch.  */
  807. #ifdef MINIX
  808. long magic;
  809. #else
  810. int magic;
  811. #endif /* MINIX */
  812.  
  813. /* Nonzero means print names of input files as processed.  */
  814. int trace_files;
  815.  
  816. /* Which symbols should be stripped (omitted from the output):
  817.    none, all, or debugger symbols.  */
  818. enum { STRIP_NONE, STRIP_ALL, STRIP_DEBUGGER } strip_symbols;
  819.  
  820. /* Which local symbols should be omitted:
  821.    none, all, or those starting with L.
  822.    This is irrelevant if STRIP_NONE.  */
  823. enum { DISCARD_NONE, DISCARD_ALL, DISCARD_L } discard_locals;
  824.  
  825. /* 1 => write load map.  */
  826. int write_map;
  827.  
  828. /* 1 => write relocation into output file so can re-input it later.  */
  829. int relocatable_output;
  830.  
  831. /* 1 => assign space to common symbols even if `relocatable_output'.  */
  832. int force_common_definition;
  833.  
  834. /* 1 => atari minix special, write symbol table for mdb in out.h format */
  835. #if (defined(MINIX) || defined(atariminix))
  836. /* 1 => write out.h format output file too */
  837. int write_minix_out = 0;
  838. FILE *minix_out_filep;
  839. char minix_out_filename[80]; /* name = strcat(output_filename, ".sym") */
  840. void do_write_minix_out();
  841. #endif
  842.  
  843. /* 1 => append '16' to lib names */
  844. int mshort = 0;
  845.  
  846. int verbose = 0;
  847.  
  848. #if defined (atarist) || defined (CROSSATARI)
  849. /* atari special, specify load flags and head size */
  850. int atari_load_flags;
  851. int atari_heap_size;
  852. #endif
  853.  
  854. /* Standard directories to search for files specified by -l.  */
  855. #ifdef CROSSATARI
  856. char *standard_search_dirs[] = {CROSSLIB};
  857. #else
  858. #ifdef atariminix
  859. char *standard_search_dirs[] = {"/usr/local/lib"};
  860. #else
  861. char *standard_search_dirs[] = {"/lib", "/usr/lib", "/usr/local/lib"};
  862. #endif
  863. #endif
  864.  
  865. #if defined(atarist) || defined(CROSSATARI)
  866. /* Write GST compatible symboltable. This format allows the symbols
  867.    to be upto 22 bytes long  */
  868. int gst_symboltable;
  869.  
  870. /* This variable count the number of extended symboltable entries.
  871.    This is done to correct the size of the symboltable in the header. */
  872. int gst_symbols;
  873. #endif
  874.  
  875. /* Actual vector of directories to search;
  876.    this contains those specified with -L plus the standard ones.  */
  877. char **search_dirs;
  878.  
  879. /* Length of the vector `search_dirs'.  */
  880. int n_search_dirs;
  881.  
  882. /* Non zero means to create the output executable. */
  883. /* Cleared by nonfatal errors.  */
  884. int make_executable;
  885.  
  886. /* Force the executable to be output, even if there are non-fatal
  887.    errors */
  888. int force_executable;
  889.  
  890. /* Keep a list of any symbols referenced from the command line (so
  891.    that error messages for these guys can be generated). This list is
  892.    zero terminated. */
  893. struct glosym **cmdline_references;
  894. int cl_refs_allocated;
  895.  
  896. /*
  897.  * Structure for communication between do_file_warnings and it's
  898.  * helper routines.  Will in practice be an array of three of these:
  899.  * 0) Current line, 1) Next line, 2) Source file info.
  900.  */
  901. struct line_debug_entry
  902. {
  903.   int line;
  904.   char *filename;
  905.   struct nlist *sym;
  906. };
  907.  
  908. #if defined(__STDC__) || defined(__cplusplus)
  909. # define P_(s) s
  910. #else
  911. # define P_(s) ()
  912. #endif
  913.  
  914.  
  915. /* ld.c */
  916. int main P_((int argc, char **argv));
  917. int classify_arg P_((char *arg));
  918. int next_indirect_name P_((FILE *f, char *buf));
  919. int decode_indirect_file P_((char *iname));
  920. struct file_entry *process_indirect_file P_((struct file_entry *p, char *iname));
  921. void decode_command P_((int argc, char **argv));
  922. void add_cmdline_ref P_((struct glosym *sp));
  923. void decode_option P_((char *swt, char *arg));
  924. void each_file P_((void (*function )(), void *arg));
  925. unsigned long check_each_file P_((unsigned long (*function )(), void *arg));
  926. void each_full_file P_((void (*function )(), void *arg));
  927. void file_close P_((void));
  928. FILE *file_open P_((struct file_entry *entry));
  929. void prline_file_name P_((struct file_entry *entry, FILE *outfile));
  930. void print_file_name P_((struct file_entry *entry, FILE *outfile));
  931. char *get_file_name P_((struct file_entry *entry));
  932. void read_header P_((FILE *desc, struct file_entry *entry));
  933. void read_entry_symbols P_((FILE *desc, struct file_entry *entry));
  934. void read_entry_strings P_((FILE *desc, struct file_entry *entry));
  935. void load_symbols P_((void));
  936. void read_file_symbols P_((struct file_entry *entry));
  937. void enter_file_symbols P_((struct file_entry *entry));
  938. void enter_global_ref P_((struct nlist *nlist_p, char *name, struct file_entry *entry));
  939. unsigned long contains_symbol P_((struct file_entry *entry, struct nlist *n_ptr));
  940. void search_library P_((FILE *desc, struct file_entry *entry));
  941. struct file_entry *decode_library_subfile P_((FILE *desc, struct file_entry *library_entry, int subfile_offset, int *length_loc));
  942. void symdef_library P_((FILE *desc, struct file_entry *entry, int member_length));
  943. void linear_library P_((FILE *desc, struct file_entry *entry));
  944. int subfile_wanted_p P_((struct file_entry *entry));
  945. void digest_symbols P_((void));
  946. void consider_file_section_lengths P_((struct file_entry *entry));
  947. void relocate_file_addresses P_((struct file_entry *entry));
  948. void print_symbols P_((FILE *outfile));
  949. void describe_file_sections P_((struct file_entry *entry, FILE *outfile));
  950. void list_file_locals P_((struct file_entry *entry, FILE *outfile));
  951. void do_file_warnings P_((struct file_entry *entry, FILE *outfile));
  952. int relocation_entries_relation P_((struct relocation_info *rel1, struct relocation_info *rel2));
  953. int next_debug_entry P_((int use_data_symbols, struct line_debug_entry state_pointer[]));
  954. int address_to_line P_((unsigned long address, struct line_debug_entry state_pointer[]));
  955. struct line_debug_entry *init_debug_scan P_((int use_data_symbols, struct file_entry *entry));
  956. void mark_flagged_symbols P_((struct file_entry *entry, FILE *outfile));
  957. void do_warnings P_((FILE *outfile));
  958. void print_files_defining_symbol P_((struct nlist *first_def, struct nlist *rest_of_refs));
  959. void write_output P_((void));
  960. void write_header P_((void));
  961. void write_text P_((void));
  962. int text_offset P_((struct file_entry *entry));
  963. void read_relocation P_((void));
  964. void read_file_relocation P_((struct file_entry *entry));
  965. void copy_text P_((struct file_entry *entry));
  966. void write_data P_((void));
  967. void copy_data P_((struct file_entry *entry));
  968. void perform_relocation P_((char *data, int pc_relocation, int data_size,
  969.     struct relocation_info *reloc_info, int reloc_size,
  970.     struct file_entry *entry, int pure_text));
  971. void write_rel P_((void));
  972. void flush_atari_rbuf P_((void));
  973. int reloc_less_p P_((struct relocation_info *reloc_1, struct relocation_info *reloc_2));
  974. void emit_atari_text_relocs P_((struct file_entry *entry));
  975. void emit_atari_data_relocs P_((struct file_entry *entry));
  976. void emit_atari_set_vectors P_((void));
  977. void coptxtrel P_((struct file_entry *entry));
  978. void copdatrel P_((struct file_entry *entry));
  979. void emit_atari_reloc_byte P_((unsigned int b));
  980. int emit_atari_reloc P_((long rel_pc));
  981. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  982. #if (!(defined(MINIX) || defined(atariminix)))
  983. void write_atari_sym P_((struct nlist *p, char *str));
  984. #else
  985. void  write_atari_sym P_((struct nlist *p, char *str, int gflag));
  986. #endif
  987. void write_atari_syms P_((struct file_entry *entry, int *syms_written_addr));
  988. #else  /* for Gnu/Unix */
  989. void write_string_table P_((void));
  990. void write_file_syms P_((struct file_entry *entry, int *syms_written_addr));
  991. #endif /* (defined(CROSSATARI) || defined(atarist) || defined(atariminix)) */
  992. void write_syms P_((void));
  993. int assign_string_table_index P_((char *name));
  994. void write_symsegs P_((void));
  995. void write_file_symseg P_((struct file_entry *entry));
  996. void symtab_init P_((void));
  997. int hash_string P_((char *key));
  998. symbol * getsym P_((char *key));
  999. symbol * getsym_soft P_((char *key));
  1000. void fatal P_((char *string, ...));
  1001. void fatal_with_file P_((char *string, struct file_entry *entry));
  1002. void perror_name P_((char *name));
  1003. void perror_file P_((struct file_entry *entry));
  1004. void mywrite P_((void *buf, int count, int eltsize, FILE *desc));
  1005. void padfile P_((int padding, FILE *outdesc));
  1006. char *concat P_((char *s1, char *s2, char *s3));
  1007. int parse P_((char *arg, char *format, char *error));
  1008. void *xmalloc P_((int size));
  1009. void *xrealloc P_((void *ptr, int size));
  1010. int write_minix_sym P_((struct nlist *p, char *str, int gflag, int fflag));
  1011. void write_minix_syms P_((struct file_entry *entry, int *syms_written_addr));
  1012. void do_write_minix_out P_((void));
  1013. unsigned short minix_swap_short P_((unsigned char s[]));
  1014. long minix_swap_long P_((unsigned char s[]));
  1015.  
  1016. #undef P_
  1017.  
  1018. #ifndef bcopy
  1019. void bcopy (), bzero ();
  1020. #endif
  1021. void *malloc (), *realloc ();
  1022. #ifndef alloca
  1023. int alloca ();
  1024. #endif
  1025.  
  1026. int free ();
  1027. void error ();
  1028. char *getenv(char *name);
  1029.  
  1030.  
  1031. int
  1032. main (argc, argv)
  1033.      char **argv;
  1034.      int argc;
  1035. {
  1036. #ifdef atarist
  1037.   _binmode(1);
  1038. #endif
  1039.  
  1040.   page_size = getpagesize ();
  1041.   if (argv[0][0] != '\0')
  1042.     progname = argv[0];
  1043.  
  1044.   /* Clear the cumulative info on the output file.  */
  1045.  
  1046.   text_size = 0;
  1047.   data_size = 0;
  1048.   bss_size = 0;
  1049.   text_reloc_size = 0;
  1050.   data_reloc_size = 0;
  1051.  
  1052.   data_pad = 0;
  1053.   text_pad = 0;
  1054.  
  1055.   /* Initialize the data about options.  */
  1056.  
  1057. #if defined(atarist) || defined(CROSSATARI)
  1058.   gst_symboltable = 1;
  1059.   gst_symbols = 0;
  1060.   atari_load_flags = 7;        /* fastload / tt ram is the default */
  1061.   atari_heap_size = 0;
  1062. #endif
  1063.   specified_data_size = 0;
  1064. #ifdef atarist
  1065.   strip_symbols = STRIP_DEBUGGER;
  1066. #else
  1067.   strip_symbols = STRIP_NONE;
  1068. #endif
  1069.   trace_files = 0;
  1070.   discard_locals = DISCARD_NONE;
  1071.   entry_symbol = 0;
  1072.   write_map = 0;
  1073.   relocatable_output = 0;
  1074.   force_common_definition = 0;
  1075.   T_flag_specified = 0;
  1076.   Tdata_flag_specified = 0;
  1077.   magic = DEFAULT_MAGIC;
  1078.   make_executable = 1;
  1079.   force_executable = 0;
  1080.  
  1081.   /* Initialize the cumulative counts of symbols.  */
  1082.  
  1083.   local_sym_count = 0;
  1084.   non_L_local_sym_count = 0;
  1085.   debugger_sym_count = 0;
  1086.   undefined_global_sym_count = 0;
  1087.   set_symbol_count = 0;
  1088.   set_vector_count = 0;
  1089.   global_indirect_count = 0;
  1090.   warning_count = 0;
  1091.   common_defined_global_count = 0;
  1092.  
  1093.   /* Keep a list of symbols referenced from the command line */
  1094.   cl_refs_allocated = 10;
  1095.   cmdline_references =
  1096.     (struct glosym **) xmalloc (cl_refs_allocated
  1097.                 * sizeof(struct glosym *));
  1098.   *cmdline_references = 0;
  1099.  
  1100.   /* Completely decode ARGV.  */
  1101.  
  1102.   decode_command (argc, argv);
  1103.  
  1104. #if (defined(MINIX) || defined(atariminix))
  1105.   if(write_minix_out == 1)
  1106.   {
  1107.       strcpy(minix_out_filename, output_filename);
  1108.       strcat(minix_out_filename, ".sym");
  1109.       /* if(strlen(minix_out_filename) - strlen(output_filename) > 14)
  1110.      then you are up s*it creek;
  1111.        */
  1112.   }
  1113. #endif
  1114.  
  1115.   /* Create the symbols `etext', `edata' and `end'.  */
  1116.  
  1117. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  1118.   if (!relocatable_output)
  1119. #endif            /* always do this on atari */
  1120.     symtab_init ();
  1121.  
  1122.   /* Determine whether to count the header as part of
  1123.      the text size, and initialize the text size accordingly.
  1124.      This depends on the kind of system and on the output format selected.  */
  1125.  
  1126. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1127.   outheader.a_magic = CMAGIC;
  1128. #else
  1129.   outheader.a_magic = magic;
  1130. #endif
  1131.  
  1132. #ifdef INITIALIZE_HEADER
  1133.   INITIALIZE_HEADER;
  1134. #endif
  1135.  
  1136. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1137.   text_size = sizeof (struct aexec);
  1138. #else
  1139.   text_size = sizeof (struct exec);
  1140. #endif
  1141.  
  1142. #ifdef COFF_ENCAPSULATE
  1143.   if (relocatable_output == 0)
  1144.     {
  1145.       need_coff_header = 1;
  1146.       /* set A_ENCAP now, since it will change the values of N_TXTOFF, etc */
  1147.       outheader.a_flags |= A_ENCAP;
  1148.       text_size += sizeof (struct coffheader);
  1149.     }
  1150. #endif
  1151.  
  1152. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1153.   text_size -= A_TXTOFF (outheader);
  1154. #else
  1155.   text_size -= N_TXTOFF (outheader);
  1156. #endif
  1157.  
  1158.   if (text_size < 0)
  1159.     text_size = 0;
  1160.   entry_offset = text_size;
  1161.  
  1162. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  1163.   if (!T_flag_specified && !relocatable_output)
  1164.     text_start = N_TXTADDR (outheader);
  1165. #endif
  1166.  
  1167.   /* The text-start address is normally this far past a page boundary.  */
  1168.  
  1169.   text_start_alignment = text_start % page_size;
  1170.  
  1171.   /* Load symbols of all input files.
  1172.      Also search all libraries and decide which library members to load.  */
  1173.  
  1174.   load_symbols ();
  1175.  
  1176.   /* Compute where each file's sections go, and relocate symbols.  */
  1177.  
  1178.   digest_symbols ();
  1179.  
  1180.   /* Print error messages for any missing symbols, for any warning
  1181.      symbols, and possibly multiple definitions */
  1182.  
  1183.   do_warnings (stderr);
  1184.  
  1185.   /* Print a map, if requested.  */
  1186.  
  1187.   if (write_map) print_symbols (stdout);
  1188.  
  1189.   /* Write the output file.  */
  1190.  
  1191.   if (make_executable || force_executable)
  1192.     write_output ();
  1193.  
  1194.   exit( ! make_executable);
  1195. }
  1196.  
  1197. void decode_option ();
  1198.  
  1199. /* Analyze a command line argument.
  1200.    Return 0 if the argument is a filename.
  1201.    Return 1 if the argument is a option complete in itself.
  1202.    Return 2 if the argument is a option which uses an argument.
  1203.  
  1204.    Thus, the value is the number of consecutive arguments
  1205.    that are part of options.  */
  1206.  
  1207. int
  1208. classify_arg (arg)
  1209.      register char *arg;
  1210. {
  1211.   if (*arg != '-') return 0;
  1212.   switch (arg[1])
  1213.     {
  1214.     case 'A':
  1215.     case 'D':
  1216.     case 'e':
  1217.     case 'L':
  1218.     case 'o':
  1219.     case 'u':
  1220.     case 'y':
  1221. #if defined (atarist) || defined (CROSSATARI)
  1222.     case 'f':
  1223.     case 'h':
  1224. #endif
  1225.       if (arg[2])
  1226.     return 1;
  1227.       return 2;
  1228.  
  1229.     case 'l':
  1230.       if (arg[2])
  1231.     return 1;
  1232.       return 2;
  1233.  
  1234.     case 'T':
  1235.       if (arg[2] == 0)
  1236.     return 2;
  1237.       if (! strcmp (&arg[2], "text"))
  1238.     return 2;
  1239.       if (! strcmp (&arg[2], "data"))
  1240.     return 2;
  1241.       return 1;
  1242.     }
  1243.  
  1244.   return 1;
  1245. }
  1246.  
  1247. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1248. /* things to grok indirect files */
  1249.  
  1250. int next_indirect_name(f, buf)
  1251. FILE * f;
  1252. char * buf;
  1253. {
  1254.   char c;
  1255.   char * s = buf;
  1256.  
  1257.   for (*buf = '\0' ; (((c = fgetc(f)) != EOF) && (isspace(c))) ; )
  1258.     ;            /* skip whitespace */
  1259.   if (c == EOF) return(0);    /* lose */
  1260.   *s++ = c;
  1261.   for ( ; (((c = fgetc(f)) != EOF) && (!isspace(c))) ; )
  1262.     *s++ = c;
  1263.   *s = '\0';            /* finish it */
  1264.   return((strlen(buf) > 0));    /* win if saw any */
  1265. }
  1266.  
  1267. int decode_indirect_file(iname)
  1268. /* (declare (values n-new-files)) */
  1269. char * iname;
  1270. {
  1271.   char fn[80];        /* name buffer */
  1272.   int i;
  1273.   FILE * ifile;
  1274.  
  1275. #ifdef DEBUG
  1276.   fprintf(stderr, "decode_indirect_file('%s')\n", iname);
  1277. #endif
  1278.   if ((ifile = fopen(iname, "r")) == NULL)
  1279.     {
  1280.     fprintf (stderr, "Can't open indirect file '%s'\n", iname);
  1281.     return(0);
  1282.     }
  1283.   for (i = 0 ; next_indirect_name(ifile, fn) ; )
  1284.     i++;        /* count files */
  1285.   fclose(ifile);
  1286. #ifdef DEBUG
  1287.   fprintf(stderr, "  ->%d\n", i);
  1288. #endif
  1289.   return(i);        /* return file count */
  1290. }
  1291.  
  1292. struct file_entry * process_indirect_file(p, iname)
  1293. /* (declare (values updated_pointer)) */
  1294. struct file_entry * p;
  1295. char * iname;
  1296. {
  1297.   char fn[80];        /* name buffer */
  1298.   FILE * ifile;
  1299.  
  1300. #ifdef DEBUG
  1301.   fprintf(stderr, "process_indirect_file(%X, '%s')\n", p, iname);
  1302. #endif
  1303.   if ((ifile = fopen(iname, "r")) == NULL)
  1304.     {
  1305.     fprintf (stderr, "Can't open indirect file '%s'\n", iname);
  1306.     return(0);
  1307.     }
  1308.   for ( ; next_indirect_name(ifile, fn) ; )
  1309.     {
  1310. #ifdef DEBUG
  1311.     fprintf(stderr, "  file '%s'\n", fn);
  1312. #endif
  1313.     p->filename = concat(fn, "", "");
  1314.     p->local_sym_name = p->filename;
  1315.     p++;
  1316.     }
  1317.   fclose(ifile);
  1318. #ifdef DEBUG
  1319.   fprintf(stderr, "  ->%X\n", p);
  1320. #endif
  1321.   return(p);        /* return new pointer */
  1322. }
  1323.  
  1324. #endif
  1325.  
  1326. /* Process the command arguments,
  1327.    setting up file_table with an entry for each input file,
  1328.    and setting variables according to the options.  */
  1329.  
  1330. void
  1331. decode_command (argc, argv)
  1332.      char **argv;
  1333.      int argc;
  1334. {
  1335.   register int i;
  1336.   register struct file_entry *p;
  1337.  
  1338.   number_of_files = 0;
  1339.   output_filename = "a.out";
  1340.  
  1341.   n_search_dirs = 0;
  1342.   search_dirs = (char **) xmalloc (sizeof (char *));
  1343.  
  1344.   /* First compute number_of_files so we know how long to make file_table.  */
  1345.   /* Also process most options completely.  */
  1346.  
  1347.   for (i = 1; i < argc; i++)
  1348.     {
  1349.       register int code = classify_arg (argv[i]);
  1350.       if (code)
  1351.     {
  1352.       if (i + code > argc)
  1353.         fatal ("no argument following %s\n", argv[i]);
  1354.  
  1355.       decode_option (argv[i], argv[i+1]);
  1356.  
  1357.       if (argv[i][1] == 'l' || argv[i][1] == 'A')
  1358.         number_of_files++;
  1359.  
  1360.       i += code - 1;
  1361.     }
  1362.       else
  1363. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1364.     if (argv[i][0] == '@')        /* indirect file? */
  1365.       number_of_files += decode_indirect_file(&argv[i][1]);
  1366.      else
  1367. #endif
  1368.     number_of_files++;
  1369.     }
  1370.  
  1371.   if (!number_of_files)
  1372.     fatal ("no input files", 0);
  1373.  
  1374.   p = file_table
  1375.     = (struct file_entry *) xmalloc (number_of_files * sizeof (struct file_entry));
  1376.   bzero (p, number_of_files * sizeof (struct file_entry));
  1377.  
  1378.   /* Now scan again and fill in file_table.  */
  1379.   /* All options except -A and -l are ignored here.  */
  1380.  
  1381.   for (i = 1; i < argc; i++)
  1382.     {
  1383.       register int code = classify_arg (argv[i]);
  1384.  
  1385.       if (code)
  1386.     {
  1387.       char *string;
  1388.       if (code == 2)
  1389.         string = argv[i+1];
  1390.       else
  1391.         string = &argv[i][2];
  1392.  
  1393.       if (argv[i][1] == 'A')
  1394.         {
  1395.           if (p != file_table)
  1396.         fatal (
  1397.          "-A specified before an input file other than the first", 0);
  1398.  
  1399.           p->filename = string;
  1400.           p->local_sym_name = string;
  1401.           p->just_syms_flag = 1;
  1402.           p++;
  1403.         }
  1404.       if (argv[i][1] == 'l')
  1405.       {
  1406. /*
  1407.  * CF: Wenn GNULIB gesetzt ist, wird davon ausgegangen, daß der Compiler
  1408.  *     unter TOS/MagiC benutzt wird und die Libs 'xxx.a' heißen.
  1409.  *     Ist GNULIB nicht gesetzt (z.B. KGMD), wird der UNIX-Standard
  1410.  *     für die Libs benutzt (libxxx.a).
  1411. */
  1412.             char    prefix[4] = "";
  1413.  
  1414.             if (getenv("GNULIB") == NULL)
  1415.                 strcpy(prefix, "lib");
  1416.  
  1417.           if (magic == NMAGIC) 
  1418.           {
  1419.             p->filename = concat(prefix, "b", string);
  1420.             if (mshort) 
  1421.               strcat(p->filename, "16");    
  1422.             strcat(p->filename, ".a");
  1423.          }
  1424.          else 
  1425.          {
  1426.             if (mshort) 
  1427.             {
  1428.                p->filename = concat(prefix, string, "16");
  1429.                strcat(p->filename, ".a");
  1430.             }
  1431.             else
  1432.                p->filename = concat(prefix, string, ".a");
  1433.          }
  1434.  
  1435.             if (verbose)
  1436.                 fprintf(stderr, "  linking: %s\n", p->filename);
  1437.  
  1438.          p->local_sym_name = concat("-l", string, "");
  1439.           p->search_dirs_flag = 1;
  1440.           p++;
  1441.      }
  1442.       i += code - 1;
  1443.     }
  1444.       else
  1445. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  1446.     if (argv[i][0] == '@')
  1447.       p = process_indirect_file(p, &argv[i][1]);
  1448.      else
  1449. #endif
  1450.     {
  1451.       p->filename = argv[i];
  1452.       p->local_sym_name = argv[i];
  1453.       p++;
  1454.     }
  1455.     }
  1456.  
  1457.   /* Now check some option settings for consistency.  */
  1458.  
  1459.   if ((magic == ZMAGIC || magic == NMAGIC)
  1460.       && (text_start - text_start_alignment) & (page_size - 1))
  1461.     fatal ("-T argument not multiple of page size, with sharable output", 0);
  1462.  
  1463. #if (defined(atarist) || defined(atariminix) || defined(CROSSATARI))
  1464.    {
  1465.      /* see if there's an env var that says where to search for things */
  1466.      char *value = getenv("GNULIB");
  1467.  
  1468.      /* Allow GNULIB to contain more than one directory, so you can take
  1469.       * advantage to your lib RAM-Disk/Harddisk/Floppy(;-)
  1470.       * The dirs are delimited by ';' or ','.        [br]
  1471.       */
  1472. #ifdef DEBUG
  1473.      fprintf(stderr, "env->%X\n", value);
  1474. #endif
  1475.      if (value) {
  1476.      char *glib = (char *)alloca(strlen(value) + 1), *cp, *cpp;
  1477.      
  1478.      strcpy(glib, value);
  1479. #ifdef DEBUG
  1480.      fprintf(stderr, "glib->'%s'\n", glib);
  1481. #endif
  1482.      cp = cpp = glib;
  1483.      while( *cpp != '\0' ) {
  1484. #ifdef atarist
  1485.          while( (*cp != ';') && (*cp != ',') && (*cp != '\0') )
  1486. #else
  1487.          while( (*cp != ':') && (*cp != '\0') )
  1488. #endif
  1489.          cp++;
  1490.          if( *cp != '\0' )
  1491.          *cp++ = '\0';    /* terminate string and advance to next    */
  1492.                  /* else  "*(cpp = cp) = '\0'"  =>> end */
  1493.          n_search_dirs++;
  1494.          search_dirs =
  1495.          (char **) xrealloc((char *)search_dirs,
  1496.                     n_search_dirs*sizeof(char *));
  1497.          search_dirs[n_search_dirs-1] = concat(cpp, "", "");
  1498. #ifdef DEBUG
  1499.          fprintf(stderr, "... '%s'\n", search_dirs[n_search_dirs-1]);
  1500. #endif
  1501.  
  1502.          cpp = cp;        /* ptr to next entry or '\0' (see above) */
  1503.      }
  1504.      }
  1505.   }
  1506. #endif  /* Atari ST special */
  1507.  
  1508.   /* Append the standard search directories to the user-specified ones.  */
  1509.   {
  1510.     int n = sizeof standard_search_dirs / sizeof standard_search_dirs[0];
  1511.     n_search_dirs += n;
  1512.     search_dirs
  1513.       = (char **) xrealloc ((char *)search_dirs,
  1514.                 n_search_dirs * sizeof (char *));
  1515.     bcopy (standard_search_dirs, &search_dirs[n_search_dirs - n],
  1516.        n * sizeof (char *));
  1517.   }
  1518. }
  1519.  
  1520.  
  1521. void
  1522. add_cmdline_ref (sp)
  1523.      struct glosym *sp;
  1524. {
  1525.   struct glosym **ptr;
  1526.  
  1527.   for (ptr = cmdline_references;
  1528.        ptr < cmdline_references + cl_refs_allocated && *ptr;
  1529.        ptr++)
  1530.     ;
  1531.  
  1532.   if (ptr == cmdline_references + cl_refs_allocated)
  1533.     {
  1534.       int diff = ptr - cmdline_references;
  1535.       
  1536.       cl_refs_allocated *= 2;
  1537.       cmdline_references = (struct glosym **)
  1538.     xrealloc ((char *)cmdline_references,
  1539.          cl_refs_allocated * sizeof (struct glosym *));
  1540.       ptr = cmdline_references + diff;
  1541.     }
  1542.   
  1543.   *ptr++ = sp;
  1544.   *ptr = (struct glosym *) 0;
  1545. }
  1546.     
  1547. int parse ();
  1548.  
  1549. /* Record an option and arrange to act on it later.
  1550.    ARG should be the following command argument,
  1551.    which may or may not be used by this option.
  1552.  
  1553.    The `l' and `A' options are ignored here since they actually
  1554.    specify input files.  */
  1555.  
  1556. void
  1557. decode_option (swt, arg)
  1558.      register char *swt, *arg;
  1559. {
  1560.   if (! strcmp (swt + 1, "Ttext"))
  1561.     {
  1562.       text_start = parse (arg, "%x", "invalid argument to -Ttext");
  1563.       T_flag_specified = 1;
  1564.       return;
  1565.     }
  1566.   if (! strcmp (swt + 1, "Tdata"))
  1567.     {
  1568.       data_start = parse (arg, "%x", "invalid argument to -Tdata");
  1569.       Tdata_flag_specified = 1;
  1570.       return;
  1571.     }
  1572.   if (! strcmp (swt + 1, "noinhibit-exec"))
  1573.     {
  1574.       force_executable = 1;
  1575.       return;
  1576.     }
  1577.  
  1578.   if (swt[2] != 0)
  1579.     arg = &swt[2];
  1580.  
  1581.   switch (swt[1])
  1582.     {
  1583.     case 'A':
  1584.       return;
  1585.  
  1586.     case 'D':
  1587.       specified_data_size = parse (arg, "%x", "invalid argument to -D");
  1588.       return;
  1589.  
  1590.     case 'd':
  1591.       force_common_definition = 1;
  1592.       return;
  1593.  
  1594.     case 'e':
  1595.       entry_symbol = getsym (arg);
  1596.       if (!entry_symbol->defined && !entry_symbol->referenced)
  1597.     undefined_global_sym_count++;
  1598.       entry_symbol->referenced = 1;
  1599.       add_cmdline_ref (entry_symbol);
  1600.       return;
  1601.  
  1602. #if defined(atarist) || defined(CROSSATARI)
  1603.     case 'G':
  1604.       gst_symboltable = 1;
  1605.       return;
  1606.  
  1607.     case 'f':
  1608.       atari_load_flags = parse (arg, "%i", "invalid argument to -f");
  1609.       return;
  1610.  
  1611.     case 'h':
  1612.       {
  1613.     int heapsize_specified = parse (arg, "%i", "invalid argument to -h");
  1614.     if (heapsize_specified >= 0 && heapsize_specified < 16)
  1615.       atari_heap_size = heapsize_specified;
  1616.     else if (heapsize_specified >= 128 && heapsize_specified <= 16 * 128)
  1617.       atari_heap_size = (heapsize_specified - 128) / 128;
  1618.     else
  1619.       fatal ("invalid heap size", 0);
  1620.       }
  1621.       return;
  1622.    
  1623.     case 'H':
  1624.       mshort = 1;
  1625.       return;
  1626.  
  1627. #endif
  1628.  
  1629.     case 'l':
  1630.       return;
  1631.  
  1632.     case 'L':
  1633.       n_search_dirs++;
  1634.       search_dirs    = (char **) xrealloc ((char *)search_dirs, n_search_dirs * sizeof (char *));
  1635.       search_dirs[n_search_dirs - 1] = arg;
  1636.       return;
  1637.  
  1638.     case 'M':
  1639.       write_map = 1;
  1640.       return;
  1641.  
  1642.     case 'N':
  1643.       magic = OMAGIC;
  1644.       return;
  1645.  
  1646.     case 'n':
  1647.       magic = NMAGIC;
  1648.       return;
  1649.  
  1650.     case 'o':
  1651.       output_filename = arg;
  1652.       return;
  1653.  
  1654.     case 'r':
  1655. #if !(defined(atarist) || defined(CROSSATARI) || defined(atariminix))
  1656.       relocatable_output = 1;
  1657.       magic = OMAGIC;
  1658.       text_start = 0;
  1659. #endif
  1660.       return;
  1661.  
  1662.     case 'S':
  1663.       strip_symbols = STRIP_DEBUGGER;
  1664.       return;
  1665.  
  1666.     case 's':
  1667.       strip_symbols = STRIP_ALL;
  1668.       return;
  1669.  
  1670.     case 'T':
  1671.       text_start = parse (arg, "%x", "invalid argument to -T");
  1672.       T_flag_specified = 1;
  1673.       return;
  1674.  
  1675.     case 't':
  1676.       trace_files = 1;
  1677.       return;
  1678.  
  1679.     case 'u':
  1680.       {
  1681.     register symbol *sp = getsym (arg);
  1682.     if (!sp->defined && !sp->referenced)
  1683.       undefined_global_sym_count++;
  1684.     sp->referenced = 1;
  1685.     add_cmdline_ref (sp);
  1686.       }
  1687.       return;
  1688.  
  1689. #if defined(atarist) || defined(CROSSATARI)
  1690.     case 'v':
  1691.         verbose = 1;
  1692.         dump_version(progname);
  1693.       return;
  1694. #endif /* atarist */
  1695.  
  1696.     case 'X':
  1697.       discard_locals = DISCARD_L;
  1698.       return;
  1699.  
  1700.     case 'x':
  1701.       discard_locals = DISCARD_ALL;
  1702.       return;
  1703.  
  1704.     case 'y':
  1705.       {
  1706.     register symbol *sp = getsym (&swt[2]);
  1707.     sp->trace = 1;
  1708.       }
  1709.       return;
  1710.  
  1711. #if (defined(MINIX) || defined(atariminix))
  1712.     case 'z':    /* ok we needed a letter */
  1713.       write_minix_out = 1;
  1714.       return;
  1715. #else
  1716.     case 'z':
  1717. #if !(defined(atarist) || defined(CROSSATARI))
  1718.       magic = ZMAGIC;
  1719. #endif
  1720.       return;
  1721. #endif
  1722.  
  1723.     default:
  1724.       fatal ("invalid command option `%s'", swt);
  1725.     }
  1726. }
  1727.  
  1728. /** Convenient functions for operating on one or all files being */
  1729.  /** loaded.  */
  1730. void print_file_name ();
  1731.  
  1732. /* Call FUNCTION on each input file entry.
  1733.    Do not call for entries for libraries;
  1734.    instead, call once for each library member that is being loaded.
  1735.  
  1736.    FUNCTION receives two arguments: the entry, and ARG.  */
  1737.  
  1738. void
  1739. each_file (function, arg)
  1740.      register void (*function)();
  1741.      register void *arg;
  1742. {
  1743.   register int i;
  1744.  
  1745.   for (i = 0; i < number_of_files; i++)
  1746.     {
  1747.       register struct file_entry *entry = &file_table[i];
  1748.       if (entry->library_flag)
  1749.         {
  1750.       register struct file_entry *subentry = entry->subfiles;
  1751.       for (; subentry; subentry = subentry->chain)
  1752.         (*function) (subentry, arg);
  1753.     }
  1754.       else
  1755.     (*function) (entry, arg);
  1756.     }
  1757. }
  1758.  
  1759. /* Call FUNCTION on each input file entry until it returns a non-zero
  1760.    value.  Return this value.
  1761.    Do not call for entries for libraries;
  1762.    instead, call once for each library member that is being loaded.
  1763.  
  1764.    FUNCTION receives two arguments: the entry, and ARG.  It must be a
  1765.    function returning unsigned long (though this can probably be fudged). */
  1766.  
  1767. unsigned long
  1768. check_each_file (function, arg)
  1769.      register unsigned long (*function)();
  1770.      register void *arg;
  1771. {
  1772.   register int i;
  1773.   register unsigned long return_val;
  1774.  
  1775.   for (i = 0; i < number_of_files; i++)
  1776.     {
  1777.       register struct file_entry *entry = &file_table[i];
  1778.       if (entry->library_flag)
  1779.         {
  1780.       register struct file_entry *subentry = entry->subfiles;
  1781.       for (; subentry; subentry = subentry->chain)
  1782.         if (return_val = (*function) (subentry, arg))
  1783.           return return_val;
  1784.     }
  1785.       else
  1786.     if (return_val = (*function) (entry, arg))
  1787.       return return_val;
  1788.     }
  1789.   return 0;
  1790. }
  1791.  
  1792. /* Like `each_file' but ignore files that were just for symbol definitions.  */
  1793.  
  1794. void
  1795. each_full_file (function, arg)
  1796.      register void (*function)();
  1797.      register void *arg;
  1798. {
  1799.   register int i;
  1800.  
  1801.   for (i = 0; i < number_of_files; i++)
  1802.     {
  1803.       register struct file_entry *entry = &file_table[i];
  1804.       if (entry->just_syms_flag)
  1805.     continue;
  1806.       if (entry->library_flag)
  1807.         {
  1808.       register struct file_entry *subentry = entry->subfiles;
  1809.       for (; subentry; subentry = subentry->chain)
  1810.         (*function) (subentry, arg);
  1811.     }
  1812.       else
  1813.     (*function) (entry, arg);
  1814.     }
  1815. }
  1816.  
  1817. /* Close the input file that is now open.  */
  1818.  
  1819. void
  1820. file_close ()
  1821. {
  1822.   fclose (input_desc);
  1823.   input_desc = 0;
  1824.   input_file = 0;
  1825. }
  1826.  
  1827. /* Open the input file specified by 'entry', and return a descriptor.
  1828.    The open file is remembered; if the same file is opened twice in a row,
  1829.    a new open is not actually done.  */
  1830.  
  1831. FILE *
  1832. file_open (entry)
  1833.      register struct file_entry *entry;
  1834. {
  1835.   register FILE *desc = NULL;
  1836.  
  1837.   if (entry->superfile)
  1838.     return file_open (entry->superfile);
  1839.  
  1840.   if (entry == input_file)
  1841.     return input_desc;
  1842.  
  1843.   if (input_file) file_close ();
  1844.  
  1845.   if (entry->search_dirs_flag && n_search_dirs)
  1846.     {
  1847. /*       register char **p = search_dirs;  */
  1848.       int i;
  1849.  
  1850.       for (i = 0; i < n_search_dirs; i++)
  1851.     {
  1852.       register char *string
  1853. #ifndef atarist
  1854.         = concat (search_dirs[i], "/", entry->filename);
  1855. #else
  1856.         = concat (search_dirs[i], "\\", entry->filename);
  1857. #endif
  1858.       desc = fopen (string, "r");
  1859.       if (desc != NULL)
  1860.         {
  1861. #ifdef _IOFBF
  1862.           if(setvbuf(desc, NULL, _IOFBF, 128L * 1024))
  1863.         perror_name(string);
  1864. #endif
  1865.           entry->filename = string;
  1866.           entry->search_dirs_flag = 0;
  1867.           break;
  1868.         }
  1869.       free (string);
  1870.     }
  1871.     }
  1872.   else
  1873.     desc = fopen (entry->filename, "r");
  1874.  
  1875.   if (desc != NULL)
  1876.     {
  1877. #ifdef _IOFBF
  1878.           if(setvbuf(desc, NULL, _IOFBF, 128L * 1024))
  1879.         perror_name(entry->filename);
  1880. #endif
  1881.       input_file = entry;
  1882.       input_desc = desc;
  1883.       return desc;
  1884.     }
  1885.  
  1886.   perror_file (entry);
  1887.   /* NOTREACHED */
  1888. }
  1889.  
  1890. /* Print the filename of ENTRY on OUTFILE (a stdio stream),
  1891.    and then a newline.  */
  1892.  
  1893. void
  1894. prline_file_name (entry, outfile)
  1895.      struct file_entry *entry;
  1896.      FILE *outfile;
  1897. {
  1898.   print_file_name (entry, outfile);
  1899.   fprintf (outfile, "\n");
  1900. }
  1901.  
  1902. /* Print the filename of ENTRY on OUTFILE (a stdio stream).  */
  1903.  
  1904. void
  1905. print_file_name (entry, outfile)
  1906.      struct file_entry *entry;
  1907.      FILE *outfile;
  1908. {
  1909.   if (entry->superfile)
  1910.     {
  1911.       print_file_name (entry->superfile, outfile);
  1912.       fprintf (outfile, "(%s)", entry->filename);
  1913.     }
  1914.   else
  1915.     fprintf (outfile, "%s", entry->filename);
  1916. }
  1917.  
  1918. /* Return the filename of entry as a string (malloc'd for the purpose) */
  1919.  
  1920. char *
  1921. get_file_name (entry)
  1922.      struct file_entry *entry;
  1923. {
  1924.   char *result, *supfile;
  1925.   if (entry->superfile)
  1926.     {
  1927.       supfile = get_file_name (entry->superfile);
  1928.       result = (char *) xmalloc (strlen (supfile)
  1929.                  + strlen (entry->filename) + 3);
  1930.       sprintf (result, "%s(%s)", supfile, entry->filename);
  1931.       free (supfile);
  1932.     }
  1933.   else
  1934.     {
  1935.       result = (char *) xmalloc (strlen (entry->filename) + 1);
  1936.       strcpy (result, entry->filename);
  1937.     }
  1938.   return result;
  1939. }
  1940.  
  1941. /* Medium-level input routines for rel files.  */
  1942.  
  1943. /* Read a file's header into the proper place in the file_entry.
  1944.    DESC is the descriptor on which the file is open.
  1945.    ENTRY is the file's entry.  */
  1946.  
  1947. void
  1948. read_header (desc, entry)
  1949.      FILE *desc;
  1950.      register struct file_entry *entry;
  1951. {
  1952.   register int len;
  1953.   struct exec *loc = (struct exec *) &entry->header;
  1954.  
  1955.   fseek (desc, entry->starting_offset, 0);
  1956.   len = fread (loc, 1,  sizeof (struct exec), desc);
  1957.   if (len != sizeof (struct exec))
  1958.     fatal_with_file ("failure reading header of ", entry);
  1959.  
  1960. #ifdef BYTE_SWAP
  1961. loc->a_info = SWAP4(loc->a_info);    /* always four (4) bytes */
  1962. if (sizeof(loc->a_text)==4)            /* does this need to be ? */
  1963. {  loc->a_text   = SWAP4(loc->a_text);
  1964.    loc->a_data   = SWAP4(loc->a_data);
  1965.    loc->a_bss    = SWAP4(loc->a_bss);
  1966.    loc->a_syms   = SWAP4(loc->a_syms);
  1967.    loc->a_entry  = SWAP4(loc->a_entry);
  1968.    loc->a_trsize = SWAP4(loc->a_trsize);
  1969.    loc->a_drsize = SWAP4(loc->a_drsize); }
  1970. else
  1971. {  loc->a_text   = SWAP4(loc->a_text);
  1972.    loc->a_data   = SWAP4(loc->a_data);
  1973.    loc->a_bss    = SWAP4(loc->a_bss);
  1974.    loc->a_syms   = SWAP4(loc->a_syms);
  1975.    loc->a_entry  = SWAP4(loc->a_entry);
  1976.    loc->a_trsize = SWAP4(loc->a_trsize);
  1977.    loc->a_drsize = SWAP4(loc->a_drsize); }
  1978. #endif /* BYTE_SWAP */
  1979.  
  1980.   if (N_BADMAG (*loc))
  1981.     fatal_with_file ("bad magic number in ", entry);
  1982.  
  1983.   entry->header_read_flag = 1;
  1984. }
  1985.  
  1986. /* Read the symbols of file ENTRY into core.
  1987.    Assume it is already open, on descriptor DESC.
  1988.    Also read the length of the string table, which follows the symbol table,
  1989.    but don't read the contents of the string table.  */
  1990.  
  1991. void
  1992. read_entry_symbols (desc, entry)
  1993.      struct file_entry *entry;
  1994.      FILE *desc;
  1995. {
  1996.   int str_size;
  1997.  
  1998.   if (!entry->header_read_flag)
  1999.     read_header (desc, entry);
  2000.  
  2001.   entry->symbols = (struct nlist *) xmalloc (entry->header.a_syms);
  2002.  
  2003.   fseek (desc, N_SYMOFF (entry->header) + entry->starting_offset, 0);
  2004.   if (entry->header.a_syms != fread (entry->symbols, 1, entry->header.a_syms, desc))
  2005.     fatal_with_file ("premature end of file in symbols of ", entry);
  2006.  
  2007.   fseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  2008.   if (sizeof str_size != fread (&str_size, 1, sizeof str_size, desc))
  2009.     fatal_with_file ("bad string table size in ", entry); /***dohicky***/
  2010.  
  2011. #ifdef BYTE_SWAP
  2012. if (sizeof(str_size)==4)
  2013.   str_size = SWAP4(str_size);
  2014. else
  2015.   str_size = SWAP2(str_size);
  2016. #endif /* BYTE_SWAP */
  2017.  
  2018.   entry->string_size = str_size;
  2019. }
  2020.  
  2021. /* Read the string table of file ENTRY into core.
  2022.    Assume it is already open, on descriptor DESC.
  2023.    Also record whether a GDB symbol segment follows the string table.  */
  2024.  
  2025. void
  2026. read_entry_strings (desc, entry)
  2027.      struct file_entry *entry;
  2028.      FILE *desc;
  2029. {
  2030.   int buffer;
  2031.  
  2032.   if (!entry->header_read_flag)
  2033.     read_header (desc, entry);
  2034.  
  2035.   fseek (desc, N_STROFF (entry->header) + entry->starting_offset, 0);
  2036.   if (entry->string_size != fread (entry->strings, 1, entry->string_size, desc))
  2037.     fatal_with_file ("premature end of file in strings of ", entry);
  2038.  
  2039.   /* While we are here, see if the file has a symbol segment at the end.
  2040.      For a separate file, just try reading some more.
  2041.      For a library member, compare current pos against total size.  */
  2042.   if (entry->superfile)
  2043.     {
  2044.       if (entry->total_size == N_STROFF (entry->header) + entry->string_size)
  2045.     return;
  2046.     }
  2047.   else
  2048.     {
  2049.       buffer = fread (&buffer, 1, sizeof buffer, desc);
  2050.       if (buffer == 0)
  2051.     return;
  2052.       if (buffer != sizeof buffer)
  2053.     fatal_with_file ("premature end of file in GDB symbol segment of ", entry);
  2054.     }
  2055.  
  2056.   entry->symseg_offset = N_STROFF (entry->header) + entry->string_size;
  2057. }
  2058.  
  2059. /* Read in the symbols of all input files.  */
  2060.  
  2061. void read_file_symbols (), read_entry_symbols (), read_entry_strings ();
  2062. void enter_file_symbols (), enter_global_ref (), search_library ();
  2063.  
  2064. void
  2065. load_symbols ()
  2066. {
  2067.   register int i;
  2068.  
  2069.   if (trace_files) fprintf (stderr, "Loading symbols:\n\n");
  2070.  
  2071.   for (i = 0; i < number_of_files; i++)
  2072.     {
  2073.       register struct file_entry *entry = &file_table[i];
  2074.       read_file_symbols (entry);
  2075.     }
  2076.  
  2077.   if (trace_files) fprintf (stderr, "\n");
  2078. }
  2079.  
  2080. /* If ENTRY is a rel file, read its symbol and string sections into core.
  2081.    If it is a library, search it and load the appropriate members
  2082.    (which means calling this function recursively on those members).  */
  2083. /* fixed for byte swapping -- ko */
  2084.  
  2085. void
  2086. read_file_symbols (entry)
  2087.      register struct file_entry *entry;
  2088. {
  2089.   register FILE *desc;
  2090.   register int len;
  2091.   int magicnum;
  2092.  
  2093.   desc = file_open (entry);
  2094.  
  2095.   len = fread ( &magicnum, 1, sizeof magicnum, desc);
  2096.   if (len != sizeof magicnum)
  2097.     fatal_with_file ("failure reading header of ", entry);
  2098.  
  2099. #ifdef BYTE_SWAP
  2100. if (sizeof(magicnum)==4)    /* does this need to be so? ko */
  2101.   magicnum = SWAP4(magicnum);
  2102. else
  2103.   magicnum = SWAP2(magicnum);
  2104. #endif
  2105.  
  2106.   if (!N_BADMAG (*((struct exec *)&magicnum)))
  2107.     {
  2108.       read_entry_symbols (desc, entry);
  2109.       entry->strings = (char *) xmalloc (entry->string_size);
  2110.       read_entry_strings (desc, entry);
  2111.       enter_file_symbols (entry);
  2112.       free (entry->strings);
  2113.       entry->strings = 0;
  2114.     }
  2115.   else
  2116.     {
  2117.       char armag[SARMAG];
  2118.  
  2119.       fseek (desc, 0, 0);
  2120.       if (SARMAG != fread (armag, 1, SARMAG, desc) || strncmp (armag, ARMAG, SARMAG))
  2121.     fatal_with_file ("malformed input file (not rel or archive) ", entry);
  2122.       entry->library_flag = 1;
  2123.       search_library (desc, entry);
  2124.     }
  2125.  
  2126.   file_close ();
  2127. }
  2128.  
  2129. /* Enter the external symbol defs and refs of ENTRY in the hash table.  */
  2130.  
  2131. void
  2132. enter_file_symbols (entry)
  2133.      struct file_entry *entry;
  2134. {
  2135.    register struct nlist
  2136.      *p,
  2137.      *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2138. #if 0
  2139.    int lowest_set_vector = -1;
  2140. #endif
  2141.  
  2142.   if (trace_files) prline_file_name (entry, stderr);
  2143.  
  2144.   for (p = entry->symbols; p < end; p++)
  2145. #ifdef BYTE_SWAP
  2146. {    /* convert the n_list ??? */
  2147. if (entry->superfile==NULL) {
  2148. p->n_un.n_strx = SWAP4(p->n_un.n_strx);    /* oochie wow-wow ??!!! -- ko */
  2149. p->n_desc      = SWAP2(p->n_desc);
  2150. p->n_value     = SWAP4(p->n_value); }
  2151. #endif /* BYTE_SWAP */
  2152.  
  2153. #if 0
  2154.     /* Currently N_SETV symbols shouldn't be in input to the loader, */
  2155.     /* but I'll leave this here in case they ever are */
  2156.     if ((p->n_type & ~N_EXT) == N_SETV &&
  2157.     (lowest_set_vector = -1 || lowest_set_vector > p->n_value))
  2158.       lowest_set_vector = p->n_value;
  2159.     else
  2160. #endif
  2161.     if (SET_ELEMENT_P (p->n_type))
  2162.       {
  2163.     set_symbol_count++;
  2164.     if (!relocatable_output)
  2165.       enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2166.       }
  2167.     else if (p->n_type == N_WARNING)
  2168.       {
  2169.       char *name = p->n_un.n_strx + entry->strings;
  2170.  
  2171.       /* Grab the next entry.  */
  2172.       p++;
  2173.  
  2174. #ifdef BYTE_SWAP
  2175. if (entry->superfile==NULL) {
  2176. p->n_un.n_strx = SWAP4(p->n_un.n_strx);    /* oochie wow-wow ??!!! -- ko */
  2177. p->n_desc      = SWAP2(p->n_desc);
  2178. p->n_value     = SWAP4(p->n_value); }
  2179. #endif /* BYTE_SWAP */
  2180.  
  2181.       if (p->n_type != (N_UNDF | N_EXT))
  2182.         {
  2183.           fprintf (stderr, "%s: Warning symbol found in %s without external reference following.\n",
  2184.                progname, entry->filename);
  2185.           make_executable = 0;
  2186.           p--;        /* Process normally.  */
  2187.         }
  2188.       else
  2189.         {
  2190.           symbol *sp;
  2191.           char *sname = p->n_un.n_strx + entry->strings;
  2192.           /* Deal with the warning symbol.  */
  2193.           enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2194.           sp = getsym (sname);
  2195.           sp->warning = (char *) xmalloc (strlen(name) + 1);
  2196.           strcpy (sp->warning, name);
  2197.           warning_count++;
  2198.         }
  2199.       }
  2200.     else if (p->n_type & N_EXT)
  2201.       enter_global_ref (p, p->n_un.n_strx + entry->strings, entry);
  2202.     else if (p->n_un.n_strx && !(p->n_type & (N_STAB | N_EXT)))
  2203.       {
  2204.     if ((p->n_un.n_strx + entry->strings)[0] != 'L')
  2205.       non_L_local_sym_count++;
  2206.     local_sym_count++;
  2207.       }
  2208.     else debugger_sym_count++;
  2209.  
  2210. #ifdef BYTE_SWAP
  2211. }
  2212. #endif /* BYTE_SWAP */
  2213. #if 0
  2214.   entry->set_vector_offset = lowest_set_vector;
  2215. #endif
  2216.  
  2217.    /* Count one for the local symbol that we generate,
  2218.       whose name is the file's name (usually) and whose address
  2219.       is the start of the file's text.  */
  2220.  
  2221.   local_sym_count++;
  2222.   non_L_local_sym_count++;
  2223. }
  2224.  
  2225. /* Enter one global symbol in the hash table.
  2226.    NLIST_P points to the `struct nlist' read from the file
  2227.    that describes the global symbol.  NAME is the symbol's name.
  2228.    ENTRY is the file entry for the file the symbol comes from.
  2229.  
  2230.    The `struct nlist' is modified by placing it on a chain of
  2231.    all such structs that refer to the same global symbol.
  2232.    This chain starts in the `refs' field of the symbol table entry
  2233.    and is chained through the `n_name'.  */
  2234.  
  2235. void
  2236. enter_global_ref (nlist_p, name, entry)
  2237.      register struct nlist *nlist_p;
  2238.      char *name;
  2239.      struct file_entry *entry;
  2240. {
  2241.   register symbol *sp = getsym (name);
  2242.   register int type = nlist_p->n_type;
  2243.   int oldref = sp->referenced;
  2244.   int olddef = sp->defined;
  2245.  
  2246.   nlist_p->n_un.n_name = (char *) sp->refs;
  2247.   sp->refs = nlist_p;
  2248.  
  2249.   sp->referenced = 1;
  2250.   if (type != (N_UNDF | N_EXT) || nlist_p->n_value)
  2251.     {
  2252.       if (!sp->defined || sp->defined == (N_UNDF | N_EXT))
  2253.     sp->defined = type;
  2254.  
  2255.       if (oldref && !olddef)
  2256.     /* It used to be undefined and we're defining it.  */
  2257.     undefined_global_sym_count--;
  2258.  
  2259. #if 0
  2260.       /* If this is a common definition, keep track of largest
  2261.      common definition seen for this symbol.  */
  2262.       if (type == (N_UNDF | N_EXT)
  2263.       && sp->max_common_size < nlist_p->n_value)
  2264.     sp->max_common_size = nlist_p->n_value;
  2265. #endif
  2266.       if (!olddef && type == (N_UNDF | N_EXT) && nlist_p->n_value)
  2267.     {
  2268.       /* First definition and it's common.  */
  2269.       common_defined_global_count++;
  2270.       sp->max_common_size = nlist_p->n_value;
  2271.     }
  2272.       else if (olddef && sp->max_common_size && type != (N_UNDF | N_EXT))
  2273.     {
  2274.       /* It used to be common and we're defining it as
  2275.          something else.  */
  2276.       common_defined_global_count--;
  2277.       sp->max_common_size = 0;
  2278.     }
  2279.       else if (olddef && sp->max_common_size && type == (N_UNDF | N_EXT)
  2280.       && sp->max_common_size < nlist_p->n_value)
  2281.     /* It used to be common and this is a new common entry to
  2282.        which we need to pay attention.  */
  2283.     sp->max_common_size = nlist_p->n_value;
  2284.  
  2285.  
  2286.       /* Are we defining it as a set element?  */
  2287. #if 0
  2288.       if (SET_ELEMENT_P (type)) /* potential fix but i dont see why ++jrb */
  2289. #endif
  2290.       if (SET_ELEMENT_P (type)
  2291.       && (!olddef || (olddef && sp->max_common_size)))
  2292.     set_vector_count++;
  2293.       /* Indirect symbols value should be modified to point
  2294.      a symbol being equivalenced to. */
  2295.       if (type == (N_INDR | N_EXT))
  2296.     {
  2297.       nlist_p->n_value =
  2298.         (unsigned int) getsym ((nlist_p + 1)->n_un.n_strx
  2299.                    + entry->strings);
  2300.       if ((symbol *) nlist_p->n_value == sp)
  2301.         {
  2302.           /* Somebody redefined a symbol to be itself.  */
  2303.           fprintf (stderr, "%s: Symbol %s indirected to itself.\n",
  2304.                entry->filename, name);
  2305.           /* Rewrite this symbol as being a global text symbol
  2306.          with value 0.  */
  2307.           nlist_p->n_type = sp->defined = N_TEXT | N_EXT;
  2308.           nlist_p->n_value = 0;
  2309.           /* Don't make the output executable.  */
  2310.           make_executable = 0;
  2311.         }
  2312.       else
  2313.             global_indirect_count++;
  2314.     }
  2315.     }
  2316.   else
  2317.     if (!oldref)
  2318. #ifndef DOLLAR_KLUDGE
  2319.       undefined_global_sym_count++;
  2320. #else
  2321.       {
  2322.     if (entry->superfile && type == (N_UNDF | N_EXT) && name[1] == '$')
  2323.       {
  2324.         /* This is an (ISI?) $-conditional; skip it */
  2325.         sp->referenced = 0;
  2326.         if (sp->trace)
  2327.           {
  2328.         fprintf (stderr, "symbol %s is a $-conditional ignored in ", sp->name);
  2329.         print_file_name (entry, stderr);
  2330.         fprintf (stderr, "\n");
  2331.           }
  2332.         return;
  2333.       }
  2334.     else
  2335.       undefined_global_sym_count++;
  2336.       }
  2337. #endif
  2338.  
  2339.   if (sp == end_symbol && entry->just_syms_flag && !T_flag_specified)
  2340.     text_start = nlist_p->n_value;
  2341.  
  2342.   if (sp->trace)
  2343.     {
  2344.       register char *reftype;
  2345.       switch (type & ~N_EXT)
  2346.     {
  2347.     case N_UNDF:
  2348.       if (nlist_p->n_value)
  2349.         reftype = "defined as common";
  2350.       else reftype = "referenced";
  2351.       break;
  2352.  
  2353.     case N_ABS:
  2354.       reftype = "defined as absolute";
  2355.       break;
  2356.  
  2357.     case N_TEXT:
  2358.       reftype = "defined in text section";
  2359.       break;
  2360.  
  2361.     case N_DATA:
  2362.       reftype = "defined in data section";
  2363.       break;
  2364.  
  2365.     case N_BSS:
  2366.       reftype = "defined in BSS section";
  2367.       break;
  2368.  
  2369.     case N_SETT:
  2370.       reftype = "is a text set element";
  2371.       break;
  2372.  
  2373.     case N_SETD:
  2374.       reftype = "is a data set element";
  2375.       break;
  2376.  
  2377.     case N_SETB:
  2378.       reftype = "is a BSS set element";
  2379.       break;
  2380.  
  2381.     case N_SETA:
  2382.       reftype = "is an absolute set element";
  2383.       break;
  2384.  
  2385.     case N_SETV:
  2386.       reftype = "defined in text section as vector";
  2387.       break;
  2388.  
  2389.     case N_INDR:
  2390.       reftype = (char *) alloca (23
  2391.                      + strlen ((nlist_p + 1)->n_un.n_strx
  2392.                            + entry->strings));
  2393.       sprintf (reftype, "defined equivalent to %s",
  2394.            (nlist_p + 1)->n_un.n_strx + entry->strings);
  2395.       break;
  2396.  
  2397.     default:
  2398.       reftype = "I don't know this type";
  2399.       break;
  2400.     }
  2401.  
  2402.       fprintf (stderr, "symbol %s %s in ", sp->name, reftype);
  2403.       print_file_name (entry, stderr);
  2404.       fprintf (stderr, "\n");
  2405.     }
  2406. }
  2407.  
  2408. /* This return 0 if the given file entry's symbol table does *not*
  2409.    contain the nlist point entry, and it returns the files entry
  2410.    pointer (cast to unsigned long) if it does. */
  2411.  
  2412. unsigned long
  2413. contains_symbol (entry, n_ptr)
  2414.      struct file_entry *entry;
  2415.      register struct nlist *n_ptr;
  2416. {
  2417.   if (n_ptr >= entry->symbols &&
  2418.       n_ptr < (entry->symbols
  2419.            + (entry->header.a_syms / sizeof (struct nlist))))
  2420.     return (unsigned long) entry;
  2421.   return 0;
  2422. }
  2423.  
  2424.  
  2425. /* Searching libraries */
  2426.  
  2427. struct file_entry *decode_library_subfile ();
  2428. void linear_library (), symdef_library ();
  2429.  
  2430. /* Search the library ENTRY, already open on descriptor DESC.
  2431.    This means deciding which library members to load,
  2432.    making a chain of `struct file_entry' for those members,
  2433.    and entering their global symbols in the hash table.  */
  2434.  
  2435. void
  2436. search_library (desc, entry)
  2437.      FILE *desc;
  2438.      struct file_entry *entry;
  2439. {
  2440.   int member_length;
  2441.   register char *name;
  2442.   register struct file_entry *subentry;
  2443.  
  2444.   if (!undefined_global_sym_count) return;
  2445.  
  2446.   /* Examine its first member, which starts SARMAG bytes in.  */
  2447.   subentry = decode_library_subfile (desc, entry, SARMAG, &member_length);
  2448.   if (!subentry) return;
  2449.  
  2450.   name = subentry->filename;
  2451.   free (subentry);
  2452.  
  2453.   /* Search via __.SYMDEF if that exists, else linearly.  */
  2454.  
  2455.   if (!strcmp (name, "__.SYMDEF"))
  2456.     symdef_library (desc, entry, member_length);
  2457.   else
  2458.     linear_library (desc, entry);
  2459. }
  2460.  
  2461. /* Construct and return a file_entry for a library member.
  2462.    The library's file_entry is library_entry, and the library is open on DESC.
  2463.    SUBFILE_OFFSET is the byte index in the library of this member's header.
  2464.    We store the length of the member into *LENGTH_LOC.  */
  2465.  
  2466. struct file_entry *
  2467. decode_library_subfile (desc, library_entry, subfile_offset, length_loc)
  2468.      FILE *desc;
  2469.      struct file_entry *library_entry;
  2470.      int subfile_offset;
  2471.      int *length_loc;
  2472. {
  2473.   int bytes_read;
  2474.   register int namelen;
  2475.   int member_length;
  2476.   register char *name;
  2477.   struct ar_hdr hdr1;
  2478.   register struct file_entry *subentry;
  2479.  
  2480.   fseek (desc, subfile_offset, 0);
  2481.  
  2482. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  2483.   bytes_read = fread (hdr1.ar_name, 1,
  2484.              sizeof (hdr1.ar_name), desc);
  2485.   bytes_read += fread (hdr1.ar_size, 1,
  2486.               sizeof (hdr1.ar_size), desc);
  2487.   bytes_read += fread (hdr1.ar_date, 1,
  2488.               sizeof (hdr1.ar_date), desc);
  2489.   bytes_read += fread (hdr1.ar_mode, 1,
  2490.               sizeof (hdr1.ar_mode), desc);
  2491.   bytes_read += fread (hdr1.ar_uid, 1,
  2492.               sizeof (hdr1.ar_uid), desc);
  2493.   bytes_read += fread (hdr1.ar_gid, 1,
  2494.               sizeof (hdr1.ar_gid), desc);
  2495.   bytes_read += fread (hdr1.ar_fmag, 1,
  2496.               sizeof (hdr1.ar_fmag), desc);
  2497. #else
  2498.   bytes_read = fread (&hdr1, 1, sizeof hdr1, desc);
  2499. #endif
  2500.   if (!bytes_read)
  2501.     return 0;        /* end of archive */
  2502.  
  2503. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  2504.   if (bytes_read  != (sizeof (hdr1.ar_name) +
  2505.               sizeof (hdr1.ar_size) +
  2506.               sizeof (hdr1.ar_date) +
  2507.               sizeof (hdr1.ar_mode) +
  2508.               sizeof (hdr1.ar_uid) +
  2509.               sizeof (hdr1.ar_gid) +
  2510.               sizeof (hdr1.ar_fmag)))
  2511.     fatal_with_file ("malformed library archive ", library_entry);
  2512. #else
  2513.   if (sizeof hdr1 != bytes_read)
  2514.     fatal_with_file ("malformed library archive ", library_entry);
  2515. #endif
  2516.  
  2517.   if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
  2518.     fatal_with_file ("malformatted header of archive member in ", library_entry);
  2519.  
  2520.   subentry = (struct file_entry *) xmalloc (sizeof (struct file_entry));
  2521.   bzero (subentry, sizeof (struct file_entry));
  2522.  
  2523.   for (namelen = 0;
  2524.        namelen < sizeof hdr1.ar_name
  2525.        && hdr1.ar_name[namelen] != 0 && hdr1.ar_name[namelen] != ' '
  2526.        && hdr1.ar_name[namelen] != '/';
  2527.        namelen++);
  2528.  
  2529.   name = (char *) xmalloc (namelen+1);
  2530.   strncpy (name, hdr1.ar_name, namelen);
  2531.   name[namelen] = 0;
  2532.  
  2533.   subentry->filename = name;
  2534.   subentry->local_sym_name = name;
  2535.   subentry->symbols = 0;
  2536.   subentry->strings = 0;
  2537.   subentry->subfiles = 0;
  2538. #if (defined(WORD_ALIGNED) && defined(CROSSATARI))
  2539.   subentry->starting_offset = subfile_offset + (sizeof (hdr1.ar_name) +
  2540.                         sizeof (hdr1.ar_size) +
  2541.                         sizeof (hdr1.ar_date) +
  2542.                         sizeof (hdr1.ar_mode) +
  2543.                         sizeof (hdr1.ar_uid) +
  2544.                         sizeof (hdr1.ar_gid) +
  2545.                         sizeof (hdr1.ar_fmag)) ;
  2546. #else
  2547.   subentry->starting_offset = subfile_offset + sizeof hdr1;
  2548. #endif
  2549.   subentry->superfile = library_entry;
  2550.   subentry->library_flag = 0;
  2551.   subentry->header_read_flag = 0;
  2552.   subentry->just_syms_flag = 0;
  2553.   subentry->chain = 0;
  2554.   subentry->total_size = member_length;
  2555.  
  2556.   (*length_loc) = member_length;
  2557.  
  2558.   return subentry;
  2559. }
  2560.  
  2561. int subfile_wanted_p ();
  2562.  
  2563. /* Search a library that has a __.SYMDEF member.
  2564.    DESC is a descriptor on which the library is open.
  2565.      The file pointer is assumed to point at the __.SYMDEF data.
  2566.    ENTRY is the library's file_entry.
  2567.    MEMBER_LENGTH is the length of the __.SYMDEF data.  */
  2568.  
  2569. void
  2570. symdef_library (desc, entry, member_length)
  2571.      FILE *desc;
  2572.      struct file_entry *entry;
  2573.      int member_length;
  2574. {
  2575.   int *symdef_data = (int *) xmalloc (member_length);
  2576.   register struct symdef *symdef_base;
  2577.   char *sym_name_base;
  2578.   int number_of_symdefs;
  2579.   int length_of_strings;
  2580.   int not_finished;
  2581.   int bytes_read;
  2582.   register int i;
  2583.   struct file_entry *prev = 0;
  2584.   int prev_offset = 0;
  2585.  
  2586.   bytes_read = fread ( symdef_data, 1, member_length, desc);
  2587.   if (bytes_read != member_length)
  2588.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2589.  
  2590. #ifdef BYTE_SWAP
  2591. if (sizeof(*symdef_data)==4)
  2592.   *symdef_data = SWAP4(*symdef_data);
  2593. else
  2594.   *symdef_data = SWAP2(*symdef_data);
  2595. #endif /* BYTE_SWAP */
  2596.  
  2597.   number_of_symdefs = *symdef_data / sizeof (struct symdef);
  2598.   if (number_of_symdefs < 0 ||
  2599.        number_of_symdefs * sizeof (struct symdef) + 2 * sizeof (int) > member_length)
  2600.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2601.  
  2602.   symdef_base = (struct symdef *) (symdef_data + 1);
  2603.  
  2604. #ifdef BYTE_SWAP
  2605.   *(int *) (symdef_base + number_of_symdefs) = SWAP4(*(int *) (symdef_base + number_of_symdefs));
  2606. #endif /* BYTE_SWAP */
  2607.  
  2608.   length_of_strings = *(int *) (symdef_base + number_of_symdefs);
  2609.  
  2610.   if (length_of_strings < 0
  2611.       || number_of_symdefs * sizeof (struct symdef) + length_of_strings
  2612.       + 2 * sizeof (int) != member_length)
  2613.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2614.  
  2615.   sym_name_base = sizeof (int) + (char *) (symdef_base + number_of_symdefs);
  2616.  
  2617.   /* Check all the string indexes for validity.  */
  2618.  
  2619.   for (i = 0; i < number_of_symdefs; i++)
  2620.     {
  2621.       register int index;
  2622.  
  2623. #ifdef BYTE_SWAP
  2624. if (sizeof(symdef_base[i].symbol_name_string_index)==4)
  2625.   symdef_base[i].symbol_name_string_index = SWAP4(symdef_base[i].symbol_name_string_index);
  2626. else
  2627.   symdef_base[i].symbol_name_string_index = SWAP2(symdef_base[i].symbol_name_string_index);
  2628. #endif /* BYTE_SWAP */
  2629.  
  2630.       index = symdef_base[i].symbol_name_string_index;
  2631.       if (index < 0 || index >= length_of_strings
  2632.       || (index && *(sym_name_base + index - 1)))
  2633.     fatal_with_file ("malformatted __.SYMDEF in ", entry);
  2634.  
  2635. #ifdef BYTE_SWAP
  2636.       if(sizeof(symdef_base[i].library_member_offset) == 4)
  2637.         symdef_base[i].library_member_offset = SWAP4(symdef_base[i].library_member_offset);
  2638.       else
  2639.         symdef_base[i].library_member_offset = SWAP2(symdef_base[i].library_member_offset);
  2640. #endif
  2641.     }
  2642.  
  2643.   /* Search the symdef data for members to load.
  2644.      Do this until one whole pass finds nothing to load.  */
  2645.  
  2646.   not_finished = 1;
  2647.   while (not_finished)
  2648.     {
  2649.       not_finished = 0;
  2650.  
  2651.       /* Scan all the symbols mentioned in the symdef for ones that we need.
  2652.      Load the library members that contain such symbols.  */
  2653.  
  2654.       for (i = 0;
  2655.        (i < number_of_symdefs
  2656.         && (undefined_global_sym_count || common_defined_global_count));
  2657.        i++)
  2658.     if (symdef_base[i].symbol_name_string_index >= 0)
  2659.       {
  2660.         register symbol *sp;
  2661.  
  2662.         sp = getsym_soft (sym_name_base
  2663.                   + symdef_base[i].symbol_name_string_index);
  2664.  
  2665.         /* If we find a symbol that appears to be needed, think carefully
  2666.            about the archive member that the symbol is in.  */
  2667.  
  2668.         if (sp && ((sp->referenced && !sp->defined)
  2669.                || (sp->defined && sp->max_common_size)))
  2670.           {
  2671.         int junk;
  2672.         register int j;
  2673.         register int offset;
  2674.         struct file_entry *subentry;
  2675.  
  2676.                 offset = symdef_base[i].library_member_offset;
  2677.  
  2678.         /* Don't think carefully about any archive member
  2679.            more than once in a given pass.  */
  2680.  
  2681.         if (prev_offset == offset)
  2682.           continue;
  2683.         prev_offset = offset;
  2684.  
  2685.         /* Read the symbol table of the archive member.  */
  2686.  
  2687.         subentry = decode_library_subfile (desc, entry, offset, &junk);
  2688.         if (subentry == 0)
  2689.           fatal ("invalid offset for %s in symbol table of %s",
  2690.              sym_name_base
  2691.              + symdef_base[i].symbol_name_string_index,
  2692.              entry->filename);
  2693.         read_entry_symbols (desc, subentry);
  2694.         subentry->strings = (char *) xmalloc (subentry->string_size);
  2695.         read_entry_strings (desc, subentry);
  2696.  
  2697.         /* Now scan the symbol table and decide whether to load.  */
  2698.  
  2699.         if (!subfile_wanted_p (subentry))
  2700.           {
  2701.             free (subentry->symbols);
  2702.             free (subentry->strings);
  2703.             free (subentry);
  2704.           }
  2705.         else
  2706.           {
  2707.             /* This member is needed; load it.
  2708.                Since we are loading something on this pass,
  2709.                we must make another pass through the symdef data.  */
  2710.  
  2711.             not_finished = 1;
  2712.  
  2713.             enter_file_symbols (subentry);
  2714.  
  2715.             if (prev)
  2716.               prev->chain = subentry;
  2717.             else entry->subfiles = subentry;
  2718.             prev = subentry;
  2719.  
  2720.             /* Clear out this member's symbols from the symdef data
  2721.                so that following passes won't waste time on them.  */
  2722.  
  2723.             for (j = 0; j < number_of_symdefs; j++)
  2724.               {
  2725.             if (symdef_base[j].library_member_offset == offset)
  2726.               symdef_base[j].symbol_name_string_index = -1;
  2727.               }
  2728.             /* We'll read the strings again if we need them again.  */
  2729.             free (subentry->strings);
  2730.             subentry->strings = 0;
  2731.           }
  2732.           }
  2733.       }
  2734.     }
  2735.  
  2736.   free (symdef_data);
  2737. }
  2738.  
  2739. /* Search a library that has no __.SYMDEF.
  2740.    ENTRY is the library's file_entry.
  2741.    DESC is the descriptor it is open on.  */
  2742.  
  2743. void
  2744. linear_library (desc, entry)
  2745.      FILE *desc;
  2746.      struct file_entry *entry;
  2747. {
  2748.   register struct file_entry *prev = 0;
  2749.   register int this_subfile_offset = SARMAG;
  2750.  
  2751.   while (undefined_global_sym_count || common_defined_global_count)
  2752.     {
  2753.       int member_length;
  2754.       register struct file_entry *subentry;
  2755.  
  2756.       subentry = decode_library_subfile (desc, entry, this_subfile_offset, &member_length);
  2757.  
  2758.       if (!subentry) return;
  2759.  
  2760.       read_entry_symbols (desc, subentry);
  2761.       subentry->strings = (char *) xmalloc (subentry->string_size);
  2762.       read_entry_strings (desc, subentry);
  2763.  
  2764.       if (!subfile_wanted_p (subentry))
  2765.     {
  2766.       free (subentry->symbols);
  2767.       free (subentry->strings);
  2768.       free (subentry);
  2769.     }
  2770.       else
  2771.     {
  2772.       enter_file_symbols (subentry);
  2773.  
  2774.       if (prev)
  2775.         prev->chain = subentry;
  2776.       else
  2777.             entry->subfiles = subentry;
  2778.       prev = subentry;
  2779.       free (subentry->strings);
  2780.           subentry->strings = 0;
  2781.     }
  2782.  
  2783.       this_subfile_offset += member_length + sizeof (struct ar_hdr);
  2784.       if (this_subfile_offset & 1) this_subfile_offset++;
  2785.     }
  2786. }
  2787.  
  2788. /* ENTRY is an entry for a library member.
  2789.    Its symbols have been read into core, but not entered.
  2790.    Return nonzero if we ought to load this member.  */
  2791.  
  2792. int
  2793. subfile_wanted_p (entry)
  2794.      struct file_entry *entry;
  2795. {
  2796.   register struct nlist *p;
  2797.   register struct nlist *end
  2798.     = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  2799. #ifdef DOLLAR_KLUDGE
  2800.   register int dollar_cond = 0;
  2801. #endif
  2802.  
  2803.   for (p = entry->symbols; p < end; p++)
  2804.     {
  2805.       register int type;
  2806.       register char * name;
  2807.  
  2808.  
  2809. #ifdef BYTE_SWAP    /* convert the n_list ??? */
  2810. p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
  2811. p->n_desc      = SWAP2(p->n_desc);
  2812. p->n_value     = SWAP4(p->n_value);
  2813. #endif /* BYTE_SWAP */
  2814.  
  2815.  
  2816.       type = p->n_type;
  2817.       name = p->n_un.n_strx + entry->strings;
  2818.  
  2819.       if (type & N_EXT && (type != (N_UNDF | N_EXT) || p->n_value
  2820. #ifdef DOLLAR_KLUDGE
  2821.                || name[1] == '$'
  2822. #endif
  2823.                ))
  2824.     {
  2825.       register symbol *sp = getsym_soft (name);
  2826.  
  2827. #ifdef DOLLAR_KLUDGE
  2828.       if (name[1] == '$')
  2829.         {
  2830.           sp = getsym_soft (&name[2]);
  2831.           dollar_cond = 1;
  2832.           if (!sp) continue;
  2833.           if (sp->referenced)
  2834.         {
  2835.           if (write_map)
  2836.             {
  2837.               print_file_name (entry, stdout);
  2838.               fprintf (stdout, " needed due to $-conditional %s\n", name);
  2839.             }
  2840. #ifdef BYTE_SWAP    /* finish converting n_list */
  2841. for (p++;p < end; p++)
  2842.   {
  2843.   p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
  2844.   p->n_desc      = SWAP2(p->n_desc);
  2845.   p->n_value     = SWAP4(p->n_value);
  2846.   }
  2847. #endif /* BYTE SWAP */
  2848.           return 1;
  2849.         }
  2850.           continue;
  2851.         }
  2852. #endif
  2853.  
  2854.       /* If this symbol has not been hashed, we can't be looking for it. */
  2855.  
  2856.       if (!sp) continue;
  2857.  
  2858.       /* Note: There has been a lot of discussion about what to
  2859.          when a common definition was previously seen (i.e. when
  2860.          sp->max_common_size > 0).
  2861.          The latest solution is to treat a previous common definition
  2862.          (wrt to subfile_wanted_p) no differently from a real definition.
  2863.          This has the advantage of simplicity and consistency: a common
  2864.          definition is just like a common definition (consistent
  2865.          with strict ANSI C) except that we allow duplicate definitions.
  2866.          Possible disadvantage: May not be the best choice for Fortran,
  2867.          though it is consistent with the standard.
  2868.  
  2869.          An earlier solution:
  2870.          We wanted to see a common definition in the subfile,
  2871.          and note its size, but ignore any other definition
  2872.          if the symbol was already defined (even as a common).
  2873.          This meant that if there were multiple common definitions,
  2874.          the final definition would use the largest size of any of them,
  2875.          as it should.  But if there was a common definition and another
  2876.          definition, like "int pipe;" in a program and "int pipe() {}"
  2877.          in the library, only the common would be used.
  2878.          Disadvantage: a poorly justified kludge.
  2879.  
  2880.          Another previous solution:
  2881.          If the symbol already had a definition as a common symbol,
  2882.          we would want this subfile if some other subfile of the
  2883.          same library that we already need anyway also used the symbol.
  2884.          This seemed like an even more ad hoc decision.
  2885.          It would also cause subfiles to be pulled in that would
  2886.          then conflict with previous entries. I.e. you couldn't
  2887.          have: ld ... start.o libc.a ... if libc.a contained start.o.
  2888.  
  2889.          Other hybrid solutions were also considered.
  2890.       */
  2891.  
  2892.       if (sp->referenced && !sp->defined)
  2893.         {
  2894.           /* This is a symbol we are looking for.  */
  2895. #ifdef DOLLAR_KLUDGE
  2896.           if (dollar_cond) continue;
  2897. #endif
  2898.           if (type == (N_UNDF | N_EXT))
  2899.         {
  2900.           /* Symbol being defined as common.
  2901.              Remember this, but don't load subfile just for this.  */
  2902.  
  2903.           common_defined_global_count++;
  2904.           sp->max_common_size = p->n_value;
  2905.           undefined_global_sym_count--;
  2906.           sp->defined = 1;
  2907.           continue;
  2908.         }
  2909.  
  2910.           if (write_map)
  2911.         {
  2912.           char *nm;
  2913.           if (demangler == NULL || (nm = (*demangler)(sp->name)) == NULL)
  2914.               nm = sp->name;
  2915.           print_file_name (entry, stdout);
  2916.           fprintf (stdout, " needed due to %s\n", nm);
  2917.           if(nm != sp->name)
  2918.              free(nm);
  2919.         }
  2920. #ifdef BYTE_SWAP        /* finish converting n_list */
  2921. for (p++;p < end; p++)
  2922.   {
  2923.   p->n_un.n_strx = SWAP4(p->n_un.n_strx); /* oochie wow-wow ??!!! -- ko */
  2924.   p->n_desc      = SWAP2(p->n_desc);
  2925.   p->n_value     = SWAP4(p->n_value);
  2926.   }
  2927. #endif /* BYTE SWAP */
  2928.           return 1;
  2929.         }
  2930.     }
  2931.     }
  2932.  
  2933.   return 0;
  2934. }
  2935.  
  2936. void consider_file_section_lengths (), relocate_file_addresses ();
  2937. void print_files_defining_symbol ();
  2938.  
  2939. /* Having entered all the global symbols and found the sizes of sections
  2940.    of all files to be linked, make all appropriate deductions from this data.
  2941.  
  2942.    We propagate global symbol values from definitions to references.
  2943.    We compute the layout of the output file and where each input file's
  2944.    contents fit into it.  */
  2945.  
  2946. void
  2947. digest_symbols ()
  2948. {
  2949.   register int i;
  2950.   int setv_fill_count;
  2951.  
  2952.   if (trace_files)
  2953.     fprintf (stderr, "Digesting symbol information:\n\n");
  2954.  
  2955.   /* Compute total size of sections */
  2956.  
  2957.   each_file (consider_file_section_lengths, 0);
  2958.  
  2959.   /* Setup the set element vector */
  2960.  
  2961.   if (!relocatable_output)
  2962.     {
  2963.       set_sect_size =
  2964.     (set_symbol_count + 2 * set_vector_count) * sizeof (unsigned long);
  2965.       set_sect_start = text_start + text_size;
  2966.       text_size += set_sect_size;
  2967.       set_vectors = (unsigned long *) xmalloc (set_sect_size);
  2968.       setv_fill_count = 0;
  2969.     }
  2970.   /* If necessary, pad text section to full page in the file.
  2971.      Include the padding in the text segment size.  */
  2972.  
  2973. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  2974.   if (magic == NMAGIC || magic == ZMAGIC)
  2975.     {
  2976.       int text_end = text_size + N_TXTOFF (outheader);
  2977.       text_pad = ((text_end + page_size - 1) & (- page_size)) - text_end;
  2978.       text_size += text_pad;
  2979.     }
  2980. #endif
  2981.  
  2982.   outheader.a_text = text_size;
  2983.  
  2984.   /* Make the data segment address start in memory on a suitable boundary.  */
  2985.  
  2986. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  2987. /* set beginning of executable's data segment to immediately after
  2988.    text segment.  This is right for the type 0x601A Tos header
  2989.    and also for the standard Stminix header 
  2990. */ 
  2991.   if (! Tdata_flag_specified)
  2992.     data_start = text_start + text_size; 
  2993. #else
  2994.   if (! Tdata_flag_specified)
  2995.     data_start = N_DATADDR (outheader) + text_start - N_TXTADDR (outheader);
  2996. #endif
  2997.  
  2998.   /* Compute start addresses of each file's sections and symbols.  */
  2999.  
  3000.   each_full_file (relocate_file_addresses, 0);
  3001.  
  3002.   /* Now, for each symbol, verify that it is defined globally at most once.
  3003.      Put the global value into the symbol entry.
  3004.      Common symbols are allocated here, in the BSS section.
  3005.      Each defined symbol is given a '->defined' field
  3006.       which is the correct N_ code for its definition,
  3007.       except in the case of common symbols with -r.
  3008.      Then make all the references point at the symbol entry
  3009.      instead of being chained together. */
  3010.  
  3011.   defined_global_sym_count = 0;
  3012.  
  3013.   for (i = 0; i < TABSIZE; i++)
  3014.     {
  3015.       register symbol *sp;
  3016.       for (sp = symtab[i]; sp; sp = sp->link)
  3017.     {
  3018.       /* For each symbol */
  3019.       register struct nlist *p, *next;
  3020.       int defs = 0, com = sp->max_common_size, erred = 0;
  3021.       struct nlist *first_definition;
  3022.       for (p = sp->refs; p; p = next)
  3023.         {
  3024.           register int type = p->n_type;
  3025.  
  3026. #if 0
  3027.           if ((type & ~N_EXT) == N_SETV) continue;
  3028. #endif
  3029.           if (SET_ELEMENT_P (type))
  3030.         {
  3031.           if (relocatable_output)
  3032.             fatal ("internal: global ref to set element with -r", 0);
  3033.           if (!defs++)
  3034.             {
  3035.               sp->value = set_sect_start
  3036.             + setv_fill_count++ * sizeof (unsigned long);
  3037.               sp->defined = N_SETV | N_EXT;
  3038.               first_definition = p;
  3039.             }
  3040.           else if ((sp->defined & ~N_EXT) != N_SETV)
  3041.             {
  3042.               make_executable = 0;
  3043.               error ("text symbol `%s' redefined as set vector.  Files:",
  3044.                  sp->name);
  3045.               print_files_defining_symbol (first_definition, p);
  3046.             }
  3047.           set_vectors[setv_fill_count++] = p->n_value;
  3048.         }
  3049.           else if ((type & N_EXT) && type != (N_UNDF | N_EXT))
  3050.         {
  3051.           /* non-common definition */
  3052.           if (defs++ && sp->value != p->n_value)
  3053.             if (!erred++)
  3054.               {
  3055.                 make_executable = 0;
  3056.             error ("multiple definitions of symbol `%s'.  Files:",
  3057.                    sp->name);
  3058.             print_files_defining_symbol (first_definition, p);
  3059.               }
  3060.           sp->value = p->n_value;
  3061.           sp->defined = type;
  3062.           first_definition = p;
  3063.         }
  3064.           next = (struct nlist *) p->n_un.n_name;
  3065.           p->n_un.n_name = (char *) sp;
  3066.         }
  3067.       /* Allocate as common if defined as common and not defined for real */
  3068.       if (com && !defs)
  3069.         {
  3070.           if (!relocatable_output || force_common_definition)
  3071.         {
  3072. #ifdef atarist
  3073.           com = (com + sizeof (short) - 1) & (- sizeof (short));
  3074. #else
  3075.           com = (com + sizeof (int) - 1) & (- sizeof (int));
  3076. #endif
  3077.  
  3078.           sp->value = data_start + data_size + bss_size;
  3079.           sp->defined = N_BSS | N_EXT;
  3080.           bss_size += com;
  3081.           if (write_map)
  3082.                   {
  3083.             char *nm;
  3084.             if (demangler == NULL || (nm = (*demangler)(sp->name)) == NULL)
  3085.               nm = sp->name;
  3086.             printf ("Allocating common %s: %x at %x\n",
  3087.                 nm, com, sp->value);
  3088.             if(nm != sp->name)
  3089.              free(nm);
  3090.                   }
  3091.         }
  3092.           else
  3093.         {
  3094.           sp->defined = 0;
  3095.           undefined_global_sym_count++;
  3096.         }
  3097.         }
  3098.       /* Set length word at front of vector and add zero at the end.
  3099.              Also reverse the vector to put it in file order. */
  3100.       if ((sp->defined & ~N_EXT) == N_SETV)
  3101.         {
  3102.           unsigned long length_word_index =
  3103.         (sp->value - set_sect_start) / sizeof (unsigned long);
  3104.               unsigned long i, tmp;
  3105.  
  3106.           set_vectors[length_word_index]
  3107.             = setv_fill_count - 1 - length_word_index;
  3108.             
  3109.           /* Reverse the vector. */
  3110.           for (i = 1;
  3111.                i < (setv_fill_count - length_word_index - 1) / 2 + 1;
  3112.                i++)
  3113.             {
  3114.               tmp = set_vectors[length_word_index + i];
  3115.               set_vectors[length_word_index + i]
  3116.                 = set_vectors[setv_fill_count - i];
  3117.               set_vectors[setv_fill_count - i] = tmp;
  3118.             }
  3119.             
  3120.           set_vectors[setv_fill_count++] = 0;
  3121.         }
  3122.       if (sp->defined)
  3123.         defined_global_sym_count++;
  3124.     }
  3125.     }
  3126.  
  3127.   if (end_symbol)        /* These are null if -r.  */
  3128.     {
  3129.       etext_symbol->value = text_size + text_start;
  3130.       edata_symbol->value = data_start + data_size;
  3131.       end_symbol->value = data_start + data_size + bss_size;
  3132.     }
  3133.  
  3134.   /* Figure the data_pad now, so that it overlaps with the bss addresses.  */
  3135.  
  3136. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3137.   if (specified_data_size && specified_data_size > data_size)
  3138.     data_pad = specified_data_size - data_size;
  3139.  
  3140.   if (magic == ZMAGIC)
  3141.     data_pad = ((data_pad + data_size + page_size - 1) & (- page_size))
  3142.                - data_size;
  3143.  
  3144.   bss_size -= data_pad;
  3145.   if (bss_size < 0) bss_size = 0;
  3146.  
  3147.   data_size += data_pad;
  3148. #endif
  3149. }
  3150.  
  3151. /* Accumulate the section sizes of input file ENTRY
  3152.    into the section sizes of the output file.  */
  3153.  
  3154. void
  3155. consider_file_section_lengths (entry)
  3156.      register struct file_entry *entry;
  3157. {
  3158.   if (entry->just_syms_flag)
  3159.     return;
  3160.  
  3161.   entry->text_start_address = text_size;
  3162.   /* If there were any vectors, we need to chop them off */
  3163. #if 0
  3164.   /* There should never be set vectors in input to the loader */
  3165.   if (entry->set_vector_offset == -1)
  3166. #endif
  3167.   text_size += entry->header.a_text;
  3168. #if 0
  3169.   else
  3170.     text_size += entry->set_vector_offset - N_TXTOFF (entry->header);
  3171. #endif
  3172.   entry->data_start_address = data_size;
  3173.   data_size += entry->header.a_data;
  3174.   entry->bss_start_address = bss_size;
  3175.   bss_size += entry->header.a_bss;
  3176.  
  3177. /* dont know if this is still needed, left it in for safety sake ++jrb */
  3178. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  3179. /* something's probably wrong elsewhere that forces this kludge.
  3180.    if bss_size turns out to be odd, add one to it */
  3181.   if (bss_size & 1)
  3182.     bss_size++;
  3183. #endif
  3184.  
  3185.   text_reloc_size += entry->header.a_trsize;
  3186.   data_reloc_size += entry->header.a_drsize;
  3187. }
  3188.  
  3189. /* Determine where the sections of ENTRY go into the output file,
  3190.    whose total section sizes are already known.
  3191.    Also relocate the addresses of the file's local and debugger symbols.  */
  3192.  
  3193. void
  3194. relocate_file_addresses (entry)
  3195.      register struct file_entry *entry;
  3196. {
  3197.   entry->text_start_address += text_start;
  3198.   /* Note that `data_start' and `data_size' have not yet been
  3199.      adjusted for `data_pad'.  If they had been, we would get the wrong
  3200.      results here.  */
  3201.   entry->data_start_address += data_start;
  3202.   entry->bss_start_address += data_start + data_size;
  3203.  
  3204.   {
  3205.     register struct nlist *p;
  3206.     register struct nlist *end
  3207.       = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3208.  
  3209.     for (p = entry->symbols; p < end; p++)
  3210.       {
  3211.     /* If this belongs to a section, update it by the section's start address */
  3212.     register int type = p->n_type & N_TYPE;
  3213.  
  3214.     switch (type)
  3215.       {
  3216.       case N_TEXT:
  3217.       case N_SETV:
  3218.       case N_SETT:
  3219.         p->n_value += entry->text_start_address;
  3220.         break;
  3221.       case N_DATA:
  3222.       case N_SETD:
  3223.         /* A symbol whose value is in the data section
  3224.            is present in the input file as if the data section
  3225.            started at an address equal to the length of the file's text.  */
  3226.         p->n_value += entry->data_start_address - entry->header.a_text;
  3227.         break;
  3228.       case N_BSS:
  3229.       case N_SETB:
  3230.         /* likewise for symbols with value in BSS.  */
  3231.         p->n_value += entry->bss_start_address
  3232.           - entry->header.a_text - entry->header.a_data;
  3233.         break;
  3234.       }
  3235.       }
  3236.   }
  3237. }
  3238.  
  3239. void describe_file_sections (), list_file_locals ();
  3240.  
  3241. /* Print a complete or partial map of the output file.  */
  3242.  
  3243. void
  3244. print_symbols (outfile)
  3245.      FILE *outfile;
  3246. {
  3247.   register int i;
  3248.  
  3249.   fprintf (outfile, "\nFiles:\n\n");
  3250.  
  3251.   each_file (describe_file_sections, outfile);
  3252.  
  3253.   fprintf (outfile, "\nGlobal symbols:\n\n");
  3254.  
  3255.   for (i = 0; i < TABSIZE; i++)
  3256.     {
  3257.       register symbol *sp;
  3258.       for (sp = symtab[i]; sp; sp = sp->link)
  3259.     {
  3260.       char *nm = NULL;
  3261.       if ((sp->defined || sp->referenced) && (demangler != NULL))
  3262.         nm = (*demangler)(sp->name);
  3263.       if(nm == NULL)
  3264.           nm = sp->name;
  3265.       if (sp->defined == 1)
  3266.         fprintf (outfile, "  %s: common, length 0x%x\n", nm, sp->max_common_size);
  3267.       if (sp->defined)
  3268.         fprintf (outfile, "  %s: 0x%x\n", nm, sp->value);
  3269.       else if (sp->referenced)
  3270.         fprintf (outfile, "  %s: undefined\n", nm);
  3271.       if(nm != sp->name)
  3272.         free(nm);
  3273.     }
  3274.     }
  3275.  
  3276.   each_file (list_file_locals, outfile);
  3277. }
  3278.  
  3279. void
  3280. describe_file_sections (entry, outfile)
  3281.      struct file_entry *entry;
  3282.      FILE *outfile;
  3283. {
  3284.   fprintf (outfile, "  ");
  3285.   print_file_name (entry, outfile);
  3286.   if (entry->just_syms_flag)
  3287.     fprintf (outfile, " symbols only\n");
  3288.   else
  3289.     fprintf (outfile, " text %x(%x), data %x(%x), bss %x(%x) hex\n",
  3290.          entry->text_start_address, entry->header.a_text,
  3291.          entry->data_start_address, entry->header.a_data,
  3292.          entry->bss_start_address, entry->header.a_bss);
  3293. }
  3294.  
  3295. void
  3296. list_file_locals (entry, outfile)
  3297.      struct file_entry *entry;
  3298.      FILE *outfile;
  3299. {
  3300.   register struct nlist *p, *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3301.  
  3302.   entry->strings = (char *) xmalloc (entry->string_size);
  3303.   read_entry_strings (file_open (entry), entry);
  3304.  
  3305.   fprintf (outfile, "\nLocal symbols of ");
  3306.   print_file_name (entry, outfile);
  3307.   fprintf (outfile, ":\n\n");
  3308.  
  3309.   for (p = entry->symbols; p < end; p++)
  3310.     /* If this is a definition,
  3311.        update it if necessary by this file's start address.  */
  3312.     if (!((p->n_type & (N_STAB | N_EXT)) || SET_ELEMENT_P(p->n_type)))
  3313.     {
  3314.       char *s = entry->strings + p->n_un.n_strx;
  3315.       char *nm;
  3316.  
  3317.       if (demangler == NULL || (nm = (*demangler)(s)) == NULL)
  3318.           nm = s;
  3319.       fprintf (outfile, "  %s: 0x%x\n", nm, p->n_value);
  3320.       if(nm != s)
  3321.     free(nm);
  3322.       }
  3323.  
  3324.   free (entry->strings);
  3325.   entry->strings = 0;        /* All done with them.  */
  3326. }
  3327.  
  3328.  
  3329. /* Static vars for do_warnings and subroutines of it */
  3330. int list_unresolved_refs;    /* List unresolved refs */
  3331. int list_warning_symbols;    /* List warning syms */
  3332. int list_multple_defs;        /* List multiple definitions */
  3333.  
  3334.  
  3335. void qsort ();
  3336.  
  3337. struct line_debug_entry *init_debug_scan ();
  3338. int next_debug_entry ();
  3339. int address_to_line ();
  3340. int relocation_entries_relation ();
  3341.  
  3342. /* Print on OUTFILE a list of all warnings generated by references
  3343.    and/or definitions in the file ENTRY.  List source file and line
  3344.    number if possible, just the .o file if not. */
  3345.  
  3346. void
  3347. do_file_warnings (entry, outfile)
  3348.      struct file_entry *entry;
  3349.      FILE *outfile;
  3350. {
  3351.   struct relocation_info *txt_reloc, *data_reloc;
  3352.   struct line_debug_entry *state_pointer;
  3353.   register struct line_debug_entry *current, *next, *source;
  3354.   char *errfmt;                /* Assigned to generally */
  3355.                     /* static values; should not */
  3356.                     /* be written into */
  3357.   char *errmsg;                /* Assigned to malloc'd values */
  3358.                     /* and copied into; should be */
  3359.                     /* freed when done */
  3360.   int invalidate_line_number;
  3361.  
  3362.   /* Read in the files strings if they aren't available */
  3363.   if (!entry->strings)
  3364.     {
  3365.       FILE *desc;
  3366.  
  3367.       entry->strings = (char *) xmalloc (entry->string_size);
  3368.       desc = file_open (entry);
  3369.       read_entry_strings (desc, entry);
  3370.     }
  3371.  
  3372.   state_pointer = init_debug_scan (0, entry);
  3373.   current = state_pointer;
  3374.   next = state_pointer + 1;
  3375.   source = state_pointer + 2;
  3376.  
  3377.   read_file_relocation (entry);
  3378.  
  3379.   /* We need to sort the relocation info here.  Sheesh, so much effort
  3380.      for one lousy error optimization. */
  3381.  
  3382.   qsort (entry->textrel,
  3383.      entry->header.a_trsize/sizeof (struct relocation_info),
  3384.      sizeof (struct relocation_info),
  3385.      relocation_entries_relation);
  3386.  
  3387.   for (txt_reloc = entry->textrel;
  3388.        txt_reloc < (entry->textrel
  3389.             + entry->header.a_trsize/sizeof (struct relocation_info));
  3390.        txt_reloc++)
  3391.     {
  3392.       register struct nlist *s;
  3393.       register symbol *g;
  3394.       int s_index;
  3395.  
  3396.       /* If the relocation isn't resolved through a symbol, continue */
  3397.       if (!RELOC_EXTERN_P(txt_reloc))
  3398.     continue;
  3399.  
  3400.       s_index = RELOC_SYMBOL(txt_reloc);
  3401.       if (s_index < 0 )
  3402.     fatal_with_file ("bad symbol in relocation table of ", entry);
  3403.       s = &entry->symbols[s_index];
  3404.  
  3405.       /* Local symbols shouldn't ever be used by relocation info, so
  3406.      the next should be safe.
  3407.      This is, of course, wrong.  References to local BSS symbols can be
  3408.      the targets of relocation info, and they can (must) be
  3409.      resolved through symbols.  However, these must be defined properly,
  3410.      (the compiler would have caught it otherwise), so we can
  3411.      ignore these cases.  */
  3412.       if (!(s->n_type & N_EXT))
  3413.     continue;
  3414.  
  3415.       g = (symbol *) s->n_un.n_name;
  3416.  
  3417.       if (!g->defined)                 /* Reference */
  3418.     {
  3419.       if (!list_unresolved_refs ||             /* Don't list any */
  3420.           g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3421.         continue;
  3422.  
  3423.       /* Undefined symbol which we should mention */
  3424.  
  3425.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3426.         {
  3427.           errfmt = "More undefined symbol %s refs follow";
  3428.           invalidate_line_number = 1;
  3429.         }
  3430.       else
  3431.         {
  3432.           errfmt = "Undefined symbol %s referenced from text";
  3433.           invalidate_line_number = 0;
  3434.         }
  3435.     }
  3436.       else                         /* Defined */
  3437.     {
  3438.       /* Potential symbol warning here */
  3439.       if (!g->warning) continue;
  3440.  
  3441.       errfmt = g->warning;
  3442.       invalidate_line_number = 0;
  3443.     }
  3444.  
  3445.       /* If errfmt == 0, errmsg has already been defined.  */
  3446.       if (errfmt != 0)
  3447.     {
  3448.       char *nm;
  3449.  
  3450.       if (demangler == NULL || (nm = (*demangler)(g->name)) == NULL)
  3451.         nm = g->name;
  3452.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (nm) + 1);
  3453.       sprintf (errmsg, errfmt, nm);
  3454.       if (nm != g->name)
  3455.         free (nm);
  3456.     }
  3457.  
  3458.       address_to_line ( (RELOC_ADDRESS (txt_reloc)
  3459.              + entry->text_start_address),
  3460.                state_pointer);
  3461.  
  3462.       if (current->line >=0)
  3463.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3464.          invalidate_line_number ? 0 : current->line, errmsg);
  3465.       else
  3466.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3467.  
  3468.       free (errmsg);
  3469.     }
  3470.  
  3471.   free (state_pointer);
  3472.   state_pointer = init_debug_scan (1, entry);
  3473.   current = state_pointer;
  3474.   next = state_pointer + 1;
  3475.   source = state_pointer + 2;
  3476.  
  3477.   /*
  3478.    * Once again, time to sort the relocation info.
  3479.    */
  3480.  
  3481.   qsort (entry->datarel,
  3482.      entry->header.a_drsize/sizeof (struct relocation_info),
  3483.      sizeof (struct relocation_info),
  3484.      relocation_entries_relation);
  3485.  
  3486.   for (data_reloc = entry->datarel;
  3487.        data_reloc < (entry->datarel
  3488.              + entry->header.a_drsize/sizeof (struct relocation_info));
  3489.        data_reloc++)
  3490.     {
  3491.       register struct nlist *s;
  3492.       register symbol *g;
  3493.       int s_index;
  3494.       
  3495.       /* If the relocation isn't resolved through a symbol, continue */
  3496.       if (!RELOC_EXTERN_P(data_reloc))
  3497.     continue;
  3498.  
  3499.       s_index = RELOC_SYMBOL(data_reloc);
  3500.       if (s_index < 0 )
  3501.     fatal_with_file ("bad symbol in relocation table of ", entry);
  3502.       s = &entry->symbols[s_index];
  3503.  
  3504.       g = (symbol *) s->n_un.n_name;
  3505.  
  3506.       if (!g->defined)                 /* Reference */
  3507.     {
  3508.       if (!list_unresolved_refs ||             /* Don't list any */
  3509.           g->undef_refs >= MAX_UREFS_PRINTED)    /* Listed too many */
  3510.         continue;
  3511.  
  3512.       /* Undefined symbol which we should mention */
  3513.  
  3514.       if (++(g->undef_refs) == MAX_UREFS_PRINTED)
  3515.         {
  3516.           errfmt = "More undefined symbol %s refs follow";
  3517.           invalidate_line_number = 1;
  3518.         }
  3519.       else
  3520.         {
  3521.           errfmt = "Undefined symbol %s referenced from data";
  3522.           invalidate_line_number = 0;
  3523.         }
  3524.     }
  3525.       else                         /* Defined */
  3526.     {
  3527.       /* Potential symbol warning here */
  3528.       if (!g->warning) continue;
  3529.  
  3530.       errfmt = g->warning;
  3531.       invalidate_line_number = 0;
  3532.     }
  3533.       
  3534.       errmsg = (char *) xmalloc (strlen (errfmt) + strlen (g->name) + 1);
  3535.       sprintf(errmsg, errfmt, g->name);
  3536.       address_to_line ( (RELOC_ADDRESS (data_reloc)
  3537.              + entry->text_start_address),
  3538.                state_pointer);
  3539.  
  3540.       if (current->line >=0)
  3541.     fprintf (outfile, "%s:%d: %s\n", current->filename,
  3542.          invalidate_line_number ? 0 : current->line, errmsg);
  3543.       else
  3544.     fprintf (outfile, "%s: %s\n", current->filename, errmsg);
  3545.  
  3546.       free (errmsg);
  3547.     }
  3548.  
  3549.   /* now scan in the non-tex/data sections for undefs */
  3550.   {  
  3551.       struct nlist *s,
  3552.       *end = entry->symbols + entry->header.a_syms / sizeof (struct nlist);
  3553.       symbol *g;
  3554.       
  3555.       for (s = entry->symbols; s < end; s++)
  3556.       {
  3557.           if (!(s->n_type & N_EXT))
  3558.               continue;
  3559.  
  3560.           g = (symbol *) s->n_un.n_name;
  3561.  
  3562.           if(g->defined)
  3563.               continue;
  3564.  
  3565.           if (g->referenced && (g->undef_refs == 0))
  3566.           {
  3567.               char *nm = NULL;
  3568.               if(demangler)
  3569.                   nm = (*demangler)(g->name);
  3570.               if(nm == NULL)
  3571.                   nm = g->name;
  3572.               print_file_name (entry, outfile);
  3573.               fprintf (outfile, ": Undefined symbol \"%s\"\n", nm);
  3574.               if(nm != g->name) free(nm);
  3575.           }
  3576.       }
  3577.   }
  3578.  
  3579.   free (entry->strings);
  3580.   entry->strings = 0;
  3581. }
  3582. /*
  3583.  * Helper routines for do_file_warnings.
  3584.  */
  3585.  
  3586. /*
  3587.  * Return an integer less than, equal to, or greater than 0 as per the
  3588.  * relation between the two relocation entries.
  3589.  * Used by qsort.
  3590.  */
  3591. int
  3592. relocation_entries_relation (rel1, rel2)
  3593.      struct relocation_info *rel1, *rel2;
  3594. {
  3595.   return RELOC_ADDRESS(rel1) - RELOC_ADDRESS(rel2);
  3596. }
  3597.  
  3598. /*
  3599.  * Takes the values in state_pointer and moves onward to the next
  3600.  * debug line symbol.  It assumes that state_pointer[1] is valid; ie
  3601.  * that it.sym points into some entry in the symbol table.  If
  3602.  * state_pointer[1].sym == 0, this routine should not be called.
  3603.  */
  3604.  
  3605. int
  3606. next_debug_entry (use_data_symbols, state_pointer)
  3607.      register int use_data_symbols;
  3608.      struct line_debug_entry state_pointer[3]; /* Must be passed by reference! */
  3609. {
  3610.   register struct line_debug_entry
  3611.     *current = state_pointer,
  3612.     *next = state_pointer + 1,
  3613.     *source = state_pointer + 2; /* Used to store source file */
  3614.   struct file_entry *entry = (struct file_entry *) source->sym;
  3615.  
  3616.   current->sym = next->sym;
  3617.   current->line = next->line;
  3618.   current->filename = next->filename;
  3619.  
  3620.   while (++(next->sym) < (entry->symbols
  3621.               + entry->header.a_syms/sizeof (struct nlist)))
  3622.     {
  3623.       switch (next->sym->n_type)
  3624.     {
  3625.     case N_SLINE:
  3626.       if (use_data_symbols) continue;
  3627.       next->line = next->sym->n_desc;
  3628.       return 1;
  3629.     case N_DSLINE:
  3630.       if (!use_data_symbols) continue;
  3631.       next->line = next->sym->n_desc;
  3632.       return 1;
  3633. #ifdef HAVE_SUN_STABS
  3634.     case N_EINCL:
  3635.       next->filename = source->filename;
  3636.       continue;
  3637. #endif
  3638.     case N_SO:
  3639.       source->filename = next->sym->n_un.n_strx + entry->strings;
  3640.       source->line++;
  3641. #ifdef HAVE_SUN_STABS
  3642.     case N_BINCL:
  3643. #endif
  3644.     case N_SOL:
  3645.       next->filename =
  3646.         next->sym->n_un.n_strx + entry->strings;
  3647.     default:
  3648.       continue;
  3649.     }
  3650.     }
  3651.   next->sym = (struct nlist *) 0;
  3652.   return 0;
  3653. }
  3654.  
  3655. int
  3656. address_to_line (address, state_pointer)
  3657.      unsigned long address;
  3658.      /* Next must be passed by reference! */
  3659.      struct line_debug_entry state_pointer[3];
  3660. {
  3661.   struct line_debug_entry
  3662.     *current = state_pointer,
  3663.     *next = state_pointer + 1;
  3664.   struct line_debug_entry *tmp_pointer;
  3665.  
  3666.   int use_data_symbols;
  3667.  
  3668.   if (next->sym)
  3669.     use_data_symbols = (next->sym->n_type & ~N_EXT) == N_DATA;
  3670.   else
  3671.     return current->line;
  3672.  
  3673.   /* Go back to the beginning if we've already passed it.  */
  3674.   if (current->sym->n_value > address)
  3675.     {
  3676.       tmp_pointer = init_debug_scan (use_data_symbols,
  3677.                      (struct file_entry *)
  3678.                      ((state_pointer + 2)->sym));
  3679.       state_pointer[0] = tmp_pointer[0];
  3680.       state_pointer[1] = tmp_pointer[1];
  3681.       state_pointer[2] = tmp_pointer[2];
  3682.       free (tmp_pointer);
  3683.     }
  3684.  
  3685.   /* If we're still in a bad way, return -1, meaning invalid line.  */
  3686.   if (current->sym->n_value > address)
  3687.     return -1;
  3688.  
  3689.   while (next->sym
  3690.      && next->sym->n_value <= address
  3691.      && next_debug_entry (use_data_symbols, state_pointer))
  3692.     ;
  3693.   return current->line;
  3694. }
  3695.  
  3696. struct line_debug_entry *
  3697. init_debug_scan (use_data_symbols, entry)
  3698.      int use_data_symbols;
  3699.      struct file_entry *entry;
  3700. {
  3701.   struct line_debug_entry
  3702.     *state_pointer =
  3703.       (struct line_debug_entry *) xmalloc (3 * sizeof (struct line_debug_entry));
  3704.   register struct line_debug_entry
  3705.     *current = state_pointer,
  3706.     *next = state_pointer + 1,
  3707.     *source = state_pointer + 2; /* Used to store source file */
  3708.  
  3709.   struct nlist *tmp;
  3710.  
  3711.   for (tmp = entry->symbols;
  3712.        tmp < (entry->symbols
  3713.           + entry->header.a_syms/sizeof (struct nlist));
  3714.        tmp++)
  3715.     if (tmp->n_type == (int) N_SO) break;
  3716.  
  3717.   if (tmp >= (entry->symbols
  3718.           + entry->header.a_syms/sizeof (struct nlist)))
  3719.     {
  3720.       /* I believe this translates to "We lose" */
  3721.       current->filename = next->filename = entry->filename;
  3722.       current->line = next->line = -1;
  3723.       current->sym = next->sym = (struct nlist *) 0;
  3724.       return state_pointer;
  3725.     }
  3726.  
  3727.   next->line = source->line = 0;
  3728.   next->filename = source->filename
  3729.     = (tmp->n_un.n_strx + entry->strings);
  3730.   source->sym = (struct nlist *) entry;
  3731.   next->sym = tmp;
  3732.  
  3733.   next_debug_entry (use_data_symbols, state_pointer); /* To setup next */
  3734.  
  3735.   if (!next->sym)        /* No line numbers for this section; */
  3736.                 /* setup output results as appropriate */
  3737.     {
  3738.       if (source->line)
  3739.     {
  3740.       current->filename = source->filename = entry->filename;
  3741.       current->line = -1;    /* Don't print lineno */
  3742.     }
  3743.       else
  3744.     {
  3745.       current->filename = source->filename;
  3746.       current->line = 0;
  3747.     }
  3748.       return state_pointer;
  3749.     }
  3750.  
  3751.  
  3752.   next_debug_entry (use_data_symbols, state_pointer); /* To setup current */
  3753.  
  3754.   return state_pointer;
  3755. }
  3756.  
  3757. void 
  3758. mark_flagged_symbols (entry, outfile)
  3759.      struct file_entry *entry;
  3760.      FILE *outfile;
  3761. {
  3762.   struct nlist *p;
  3763.   
  3764.   if (!entry->warning) return;
  3765.  
  3766.   for (p = entry->symbols;
  3767.        p < entry->symbols + (entry->header.a_syms / sizeof(struct nlist));
  3768.        p++)
  3769.     {
  3770.       unsigned char type = p->n_type;
  3771.       struct glosym *sym;
  3772.       
  3773.      /* Ignore locals and references */
  3774.       if (!(type & N_EXT) || type == N_EXT) continue;
  3775.  
  3776.       sym = (struct glosym *) p->n_un.n_name;
  3777.       if (sym->referenced)
  3778.     ((struct glosym *) p->n_un.n_name)->warning = entry->warning;
  3779.     }
  3780. }
  3781.  
  3782. void
  3783. do_warnings (outfile)
  3784.      FILE *outfile;
  3785. {
  3786.   struct glosym **clrefs;
  3787.  
  3788.   list_unresolved_refs = !relocatable_output && undefined_global_sym_count;
  3789.   list_warning_symbols = warning_count;
  3790.   list_multple_defs = 0;        /* Not currently done here */
  3791.  
  3792.   if (!(list_unresolved_refs ||
  3793.     list_warning_symbols ||
  3794.     list_multple_defs      ))
  3795.     /* No need to run this routine */
  3796.     return;
  3797.  
  3798.   if (list_warning_symbols)
  3799.     each_full_file (mark_flagged_symbols, outfile);
  3800.  
  3801.   each_full_file (do_file_warnings, outfile);
  3802.  
  3803.   if (!relocatable_output)
  3804.     for (clrefs = cmdline_references;
  3805.      clrefs < cmdline_references + cl_refs_allocated && *clrefs;
  3806.      clrefs++)
  3807.       if ((*clrefs)->referenced && !(*clrefs)->defined)
  3808.     fprintf(stderr, "Error: Unresolved reference to symbol %s\n",
  3809.         (*clrefs)->name);
  3810.  
  3811.   if (entry_symbol && !entry_symbol->defined) 
  3812.     fprintf(stderr, "%s: error: Entry symbol `%s' never defined.\n",
  3813.         progname, entry_symbol->name);
  3814.   
  3815.   fprintf (outfile, "\n");
  3816.  
  3817.   if (list_unresolved_refs || list_multple_defs)
  3818.     make_executable = 0;
  3819. }
  3820.  
  3821. /* Print the files which have the definitions for a given symbol.
  3822.    FIRST_DEF is the first nlist entry which defines the symbol, and
  3823.    REST_OF_REFS is a chain of nlist entries which may or may not be
  3824.    definitions for this symbol, but which are all references for the
  3825.    symbol.
  3826.  
  3827.    We do the job in this clumsy fashion because of the state our data
  3828.    structures are in in the middle of digest_symbols (from which this
  3829.    is called). */
  3830.  
  3831. void
  3832. print_files_defining_symbol (first_def, rest_of_refs)
  3833.      struct nlist *first_def, *rest_of_refs;
  3834. {
  3835.   struct file_entry *holder;
  3836.   struct nlist *n_ptr;
  3837.  
  3838.   if (holder =
  3839.       (struct file_entry *) check_each_file (contains_symbol, first_def))
  3840.     {
  3841.       fprintf (stderr, " ");
  3842.       prline_file_name (holder, stderr);
  3843.     }
  3844.   else
  3845.     fatal ("internal: file not found containing nlist entry", 0);
  3846.  
  3847.   for (n_ptr = rest_of_refs;
  3848.        n_ptr;
  3849.        n_ptr = (struct nlist *) n_ptr->n_un.n_name)
  3850.     if (n_ptr->n_type & ~N_EXT)
  3851.       if (holder =
  3852.       (struct file_entry *) check_each_file (contains_symbol, n_ptr))
  3853.     {
  3854.       fprintf (stderr, " ");
  3855.       prline_file_name (holder, stderr);
  3856.     }
  3857.       else
  3858.     fatal ("internal: file not found containing nlist entry", 0);
  3859. }
  3860.  
  3861. /* Write the output file */
  3862.  
  3863. void
  3864. write_output ()
  3865. {
  3866.   struct stat statbuf;
  3867.   int filemode;
  3868.  
  3869. /* #ifdef __OLD_OPEN_P__ */
  3870. #if defined(atariminix)
  3871.   /* this is for the benefit people who have a minix kernel without 3 arg open
  3872.      support (ie. no poole fs). previously it was taken care of
  3873.      by emul_open3, but i threw that away, and now assume you have
  3874.      open3 support in the kernel. so this code is just a quick and
  3875.      dirty hack to get a working gcc-ld so that you can then bootstrap
  3876.      a kernel with the poole fs
  3877.   */
  3878.   unlink(output_filename);    /* never mind if it does'nt exist */
  3879.   outdesc = fopen (output_filename, "w+");
  3880.   if (outdesc == NULL) perror_name (output_filename);
  3881. #else
  3882.   outdesc = fopen (output_filename, "w+");
  3883.   if (outdesc == NULL) perror_name (output_filename);
  3884. #endif
  3885.  
  3886. #ifdef _IOFBF
  3887.  if(setvbuf(outdesc, NULL, _IOFBF, 128L * 1024))
  3888.     perror_name(output_filename);
  3889. #endif
  3890.  
  3891.   if (fstat (fileno(outdesc), &statbuf) < 0)
  3892.     perror_name (output_filename);
  3893.  
  3894.   filemode = statbuf.st_mode;
  3895.  
  3896.   chmod (output_filename, filemode & ~0111);
  3897.  
  3898.   /* Output the a.out header.  */
  3899.   write_header ();
  3900.  
  3901.   /* Output the text and data segments, relocating as we go.  */
  3902.   write_text ();
  3903.   write_data ();
  3904.  
  3905. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3906.   /* Output the merged relocation info, if requested with `-r'.  */
  3907.   if (relocatable_output)
  3908.     write_rel ();
  3909.  
  3910.   /* Output the symbol table (both globals and locals).  */
  3911.   write_syms ();
  3912. #else
  3913. /* atari has symtab section before relocs */
  3914.   write_syms();
  3915.   write_rel();
  3916.   if (gst_symboltable && gst_symbols) 
  3917.     {
  3918.       fseek (outdesc, 0L, 0);    /* seek to beginning of file */
  3919.       write_header ();        /* rewrite header */
  3920.     }
  3921. #endif
  3922.  
  3923.   /* Copy any GDB symbol segments from input files.  */
  3924. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3925.   /* see sym-ld.c for atari */
  3926.   write_symsegs ();
  3927. #endif
  3928.  
  3929.   fclose (outdesc);
  3930.  
  3931.   chmod (output_filename, filemode | 0111);
  3932.  
  3933. #if (defined(MINIX) || defined(atariminix))
  3934.   /* write out.h format stuff out for MDB if requested */
  3935.   if(write_minix_out == 1)
  3936.       do_write_minix_out();
  3937. #endif  
  3938. }
  3939.  
  3940. void modify_location (), perform_relocation (), copy_text (), copy_data ();
  3941.  
  3942. void
  3943. write_header ()
  3944. {
  3945. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3946.   outheader.a_magic = magic;
  3947. #endif
  3948.   outheader.a_text = text_size;
  3949.   outheader.a_data = data_size;
  3950.   outheader.a_bss = bss_size;
  3951. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  3952.   outheader.a_entry = (entry_symbol ? entry_symbol->value
  3953.                : text_start + entry_offset);
  3954. #else
  3955.  
  3956. #if (defined(MINIX) || defined(atariminix))
  3957.   outheader.a_versn = A_VERSION;
  3958.   outheader.a_entry = 0;
  3959.   {
  3960.       register long stack = 0x00010000L - (outheader.a_text +
  3961.                            outheader.a_data +
  3962.                            outheader.a_bss);
  3963.       while(stack < 0) stack += 0x00010000L;
  3964.       
  3965.       outheader.a_msize = stack + (outheader.a_text + outheader.a_data +
  3966.                        outheader.a_bss);
  3967.   }
  3968.   
  3969. #else 
  3970.   /* TOS */
  3971.   outheader.a_AZero1 = 0;        /* good a place as any to do this */
  3972.   outheader.a_ldflgs = atari_load_flags | (atari_heap_size << 28);
  3973.   if (magic == NMAGIC)
  3974.     outheader.a_ldflgs |= F_SHTEXT;    /* sharable text */
  3975.   outheader.a_isreloc = ISRELOCINFO;    /* means reloc present (!) */
  3976. #endif
  3977. #endif
  3978.  
  3979. #ifdef COFF_ENCAPSULATE
  3980.   if (need_coff_header)
  3981.     {
  3982.       /* We are encapsulating BSD format within COFF format.  */
  3983.       struct coffscn *tp, *dp, *bp;
  3984.  
  3985.       tp = &coffheader.scns[0];
  3986.       dp = &coffheader.scns[1];
  3987.       bp = &coffheader.scns[2];
  3988.  
  3989.       strcpy (tp->s_name, ".text");
  3990.       tp->s_paddr = text_start;
  3991.       tp->s_vaddr = text_start;
  3992.       tp->s_size = text_size;
  3993.       tp->s_scnptr = sizeof (struct coffheader) + sizeof (struct exec);
  3994.       tp->s_relptr = 0;
  3995.       tp->s_lnnoptr = 0;
  3996.       tp->s_nreloc = 0;
  3997.       tp->s_nlnno = 0;
  3998.       tp->s_flags = 0x20;
  3999.       strcpy (dp->s_name, ".data");
  4000.       dp->s_paddr = data_start;
  4001.       dp->s_vaddr = data_start;
  4002.       dp->s_size = data_size;
  4003.       dp->s_scnptr = tp->s_scnptr + tp->s_size;
  4004.       dp->s_relptr = 0;
  4005.       dp->s_lnnoptr = 0;
  4006.       dp->s_nreloc = 0;
  4007.       dp->s_nlnno = 0;
  4008.       dp->s_flags = 0x40;
  4009.       strcpy (bp->s_name, ".bss");
  4010.       bp->s_paddr = dp->s_vaddr + dp->s_size;
  4011.       bp->s_vaddr = bp->s_paddr;
  4012.       bp->s_size = bss_size;
  4013.       bp->s_scnptr = 0;
  4014.       bp->s_relptr = 0;
  4015.       bp->s_lnnoptr = 0;
  4016.       bp->s_nreloc = 0;
  4017.       bp->s_nlnno = 0;
  4018.       bp->s_flags = 0x80;
  4019.  
  4020.       coffheader.f_magic = COFF_MAGIC;
  4021.       coffheader.f_nscns = 3;
  4022.       coffheader.f_timdat = 0;
  4023.       coffheader.f_symptr = 0;
  4024.       coffheader.f_nsyms = 0;
  4025.       coffheader.f_opthdr = 28;
  4026.       coffheader.f_flags = 0x103;
  4027.       /* aouthdr */
  4028.       coffheader.magic = ZMAGIC;
  4029.       coffheader.vstamp = 0;
  4030.       coffheader.tsize = tp->s_size;
  4031.       coffheader.dsize = dp->s_size;
  4032.       coffheader.bsize = bp->s_size;
  4033.       coffheader.entry = outheader.a_entry;
  4034.       coffheader.text_start = tp->s_vaddr;
  4035.       coffheader.data_start = dp->s_vaddr;
  4036.     }
  4037. #endif
  4038.  
  4039. #ifdef INITIALIZE_HEADER
  4040.   INITIALIZE_HEADER;
  4041. #endif
  4042.  
  4043.   if (strip_symbols == STRIP_ALL)
  4044.     nsyms = 0;
  4045.   else
  4046.     {
  4047.       nsyms = (defined_global_sym_count
  4048.            + undefined_global_sym_count);
  4049.       if (discard_locals == DISCARD_L)
  4050.     nsyms += non_L_local_sym_count;
  4051.       else if (discard_locals == DISCARD_NONE)
  4052.     nsyms += local_sym_count;
  4053.       /* One extra for following reference on indirects */
  4054.       if (relocatable_output)
  4055.     nsyms += set_symbol_count + global_indirect_count;
  4056. #if defined(atarist) || defined(CROSSATARI)
  4057.       nsyms += set_symbol_count;  /* Cos we always output them if possible */
  4058. #endif
  4059.     }
  4060.  
  4061.   if (strip_symbols == STRIP_NONE)
  4062.     nsyms += debugger_sym_count;
  4063.  
  4064. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4065.   outheader.a_syms = nsyms * sizeof (struct nlist);
  4066. #else
  4067. # ifndef WORD_ALIGNED
  4068.   outheader.a_syms = (nsyms + gst_symbols) * sizeof (struct asym);
  4069. # else
  4070. #   ifdef MINIX
  4071.       outheader.a_syms = (nsyms + gst_symbols) * 16;
  4072. #   else
  4073.       outheader.a_syms = (nsyms + gst_symbols) * 14;
  4074. #   endif
  4075. # endif
  4076. #endif
  4077.  
  4078. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4079.   if (relocatable_output)
  4080.     {
  4081.       outheader.a_trsize = text_reloc_size;
  4082.       outheader.a_drsize = data_reloc_size;
  4083.     }
  4084.   else
  4085.     {
  4086.       outheader.a_trsize = 0;
  4087.       outheader.a_drsize = 0;
  4088.     }
  4089. #endif
  4090.  
  4091. #ifdef COFF_ENCAPSULATE
  4092.   if (need_coff_header)
  4093.     mywrite (&coffheader, sizeof coffheader, 1, outdesc);
  4094. #endif
  4095. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4096.   mywrite (&outheader, sizeof (struct exec), 1, outdesc);
  4097. #else
  4098.  
  4099. #ifdef BYTE_SWAP
  4100. outheader.a_magic   = SWAP2(outheader.a_magic);
  4101. outheader.a_text    = SWAP4(outheader.a_text);
  4102. outheader.a_data    = SWAP4(outheader.a_data);
  4103. outheader.a_bss     = SWAP4(outheader.a_bss);
  4104. outheader.a_syms    = SWAP4(outheader.a_syms);
  4105. outheader.a_AZero1  = SWAP4(outheader.a_AZero1);
  4106. outheader.a_ldflgs  = SWAP4(outheader.a_ldflgs); /* only do this once */
  4107. outheader.a_isreloc = SWAP2(outheader.a_isreloc);
  4108. #endif /* BYTE_SWAP */
  4109.  
  4110. # ifndef WORD_ALIGNED
  4111.   mywrite ((char *)&outheader, sizeof outheader, 1, outdesc);
  4112. # else
  4113. #  ifndef MINIX
  4114.      mywrite (&outheader.a_magic  , sizeof outheader.a_magic  , 1, outdesc);
  4115.      mywrite (&outheader.a_text   , sizeof outheader.a_text   , 1, outdesc);
  4116.      mywrite (&outheader.a_data   , sizeof outheader.a_data   , 1, outdesc);
  4117.      mywrite (&outheader.a_bss    , sizeof outheader.a_bss    , 1, outdesc);
  4118.      mywrite (&outheader.a_syms   , sizeof outheader.a_syms   , 1, outdesc);
  4119.      mywrite (&outheader.a_AZero1 , sizeof outheader.a_AZero1 , 1, outdesc);
  4120.      mywrite (&outheader.a_AZero2 , sizeof outheader.a_AZero2 , 1, outdesc);
  4121.      mywrite (&outheader.a_isreloc, sizeof outheader.a_isreloc, 1, outdesc);
  4122. #  else
  4123.      mywrite (&outheader.a_magic  , sizeof outheader.a_magic  , 1, outdesc);
  4124.      mywrite (&outheader.a_versn  , sizeof outheader.a_versn  , 1, outdesc);
  4125.      mywrite (&outheader.a_text   , sizeof outheader.a_text   , 1, outdesc);
  4126.      mywrite (&outheader.a_data   , sizeof outheader.a_data   , 1, outdesc);
  4127.      mywrite (&outheader.a_bss    , sizeof outheader.a_bss    , 1, outdesc);
  4128.      mywrite (&outheader.a_entry  , sizeof outheader.a_entry  , 1, outdesc);
  4129.      mywrite (&outheader.a_msize  , sizeof outheader.a_msize  , 1, outdesc);
  4130.      mywrite (&outheader.a_syms   , sizeof outheader.a_syms   , 1, outdesc);
  4131. #  endif
  4132. # endif
  4133. #endif
  4134. #ifdef BYTE_SWAP    /* switch it back !!! */
  4135. outheader.a_magic   = SWAP2(outheader.a_magic);
  4136. outheader.a_text    = SWAP4(outheader.a_text);
  4137. outheader.a_data    = SWAP4(outheader.a_data);
  4138. outheader.a_bss     = SWAP4(outheader.a_bss);
  4139. outheader.a_syms    = SWAP4(outheader.a_syms);
  4140. outheader.a_AZero1  = SWAP4(outheader.a_AZero1);
  4141. outheader.a_ldflgs = SWAP4(outheader.a_ldflgs); /* only do this once */
  4142. outheader.a_isreloc = SWAP2(outheader.a_isreloc);
  4143. #endif /* BYTE_SWAP */
  4144.  
  4145.   /* Output whatever padding is required in the executable file
  4146.      between the header and the start of the text.  */
  4147.  
  4148. #ifndef COFF_ENCAPSULATE
  4149. #  if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4150.   padfile (N_TXTOFF (outheader) - sizeof outheader, outdesc);
  4151. #  endif
  4152. #endif
  4153. }
  4154.  
  4155. /* Relocate the text segment of each input file
  4156.    and write to the output file.  */
  4157.  
  4158. void
  4159. write_text ()
  4160. {
  4161. #ifdef BYTE_SWAP
  4162. int i;
  4163. unsigned long vbuf;
  4164. #endif
  4165.   if (trace_files)
  4166.     fprintf (stderr, "Copying and relocating text:\n\n");
  4167.  
  4168.   each_full_file (copy_text, 0);
  4169.   file_close ();
  4170.  
  4171.   /* Write out the set element vectors */
  4172.  
  4173.   if (set_vector_count)
  4174. #ifdef BYTE_SWAP /* THN 94-01-27 */
  4175.     for(i=0; i < set_symbol_count + 2 * set_vector_count; ++ i){
  4176.       vbuf = SWAP4(set_vectors[i]);
  4177. /* printf("%lx -> %lx\n", set_vectors[i], vbuf); */
  4178.       mywrite((char *)&vbuf,1,sizeof(unsigned long), outdesc);
  4179.     }
  4180. #else
  4181.     mywrite ((char *) set_vectors,
  4182.          set_symbol_count + 2 * set_vector_count,
  4183.          sizeof (unsigned long), outdesc);
  4184. #endif
  4185.   if (trace_files)
  4186.     fprintf (stderr, "\n");
  4187.  
  4188. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4189.   padfile (text_pad, outdesc);
  4190. #endif
  4191. }
  4192.  
  4193. int
  4194. text_offset (entry)
  4195.      struct file_entry *entry;
  4196. {
  4197.   return entry->starting_offset + N_TXTOFF (entry->header);
  4198. }
  4199.  
  4200. /* Read in all of the relocation information */
  4201.  
  4202. void
  4203. read_relocation ()
  4204. {
  4205.   each_full_file (read_file_relocation, 0);
  4206. }
  4207.  
  4208. /* Read in the relocation sections of ENTRY if necessary */
  4209.  
  4210. void
  4211. read_file_relocation (entry)
  4212.      struct file_entry *entry;
  4213. {
  4214.   register struct relocation_info *reloc;
  4215.   FILE *desc;
  4216.   int read_return;
  4217.  
  4218.   desc = NULL;
  4219.   if (!entry->textrel)
  4220.     {
  4221.       reloc = (struct relocation_info *) xmalloc (entry->header.a_trsize);
  4222.       desc = file_open (entry);
  4223.       fseek (desc,
  4224.          text_offset (entry) + entry->header.a_text + entry->header.a_data,
  4225.          0);
  4226.       if (entry->header.a_trsize != (read_return = fread (reloc, 1, entry->header.a_trsize, desc)))
  4227.     {
  4228.       fprintf (stderr, "Return from read: %d\n", read_return);
  4229.       fatal_with_file ("premature eof in text relocatino of ", entry);
  4230.     }
  4231.       entry->textrel = reloc;
  4232.     }
  4233.  
  4234.   if (!entry->datarel)
  4235.     {
  4236.       reloc = (struct relocation_info *) xmalloc (entry->header.a_drsize);
  4237.       if (desc == NULL) desc = file_open (entry);
  4238.       fseek (desc,
  4239.          text_offset (entry) + entry->header.a_text
  4240.          + entry->header.a_data + entry->header.a_trsize,
  4241.          0);
  4242.       if (entry->header.a_drsize != fread (reloc, 1, entry->header.a_drsize, desc))
  4243.     fatal_with_file ("premature eof in text relocation of ", entry);
  4244.       entry->datarel = reloc;
  4245.     }
  4246. }
  4247.  
  4248. /* Read the text segment contents of ENTRY, relocate them,
  4249.    and write the result to the output file.
  4250.    If `-r', save the text relocation for later reuse.  */
  4251.  
  4252. void
  4253. copy_text (entry)
  4254.      struct file_entry *entry;
  4255. {
  4256.   register char *bytes;
  4257.   register FILE *desc;
  4258.   register struct relocation_info *reloc;
  4259.  
  4260.   if (trace_files)
  4261.     prline_file_name (entry, stderr);
  4262.  
  4263.   desc = file_open (entry);
  4264.  
  4265.   /* Allocate space for the file's text section */
  4266.  
  4267.   bytes = (char *) xmalloc (entry->header.a_text);
  4268.  
  4269.   /* Deal with relocation information however is appropriate */
  4270.  
  4271.   if (entry->textrel)  reloc = entry->textrel;
  4272. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4273.   else if (1)        /* always keep it on atari */
  4274. #else
  4275.   else if (relocatable_output)
  4276. #endif
  4277.     {
  4278.       read_file_relocation (entry);
  4279.       reloc = entry->textrel;
  4280.     }
  4281.   else
  4282.     {
  4283.       reloc = (struct relocation_info *) alloca (entry->header.a_trsize);
  4284.       fseek (desc, text_offset (entry) + entry->header.a_text + entry->header.a_data, 0);
  4285.       if (entry->header.a_trsize != fread (reloc, 1, entry->header.a_trsize, desc))
  4286.     fatal_with_file ("premature eof in text relocation of ", entry);
  4287.     }
  4288.  
  4289.   /* Read the text section into core.  */
  4290.  
  4291.   fseek (desc, text_offset (entry), 0);
  4292.   if (entry->header.a_text != fread ( bytes, 1, entry->header.a_text, desc))
  4293.     fatal_with_file ("premature eof in text section of ", entry);
  4294.  
  4295.  
  4296.   /* Relocate the text according to the text relocation.  */
  4297.  
  4298.   perform_relocation (bytes, entry->text_start_address, entry->header.a_text,
  4299.               reloc, entry->header.a_trsize, entry, (magic == NMAGIC));
  4300.  
  4301.   /* Write the relocated text to the output file.  */
  4302.  
  4303.   mywrite (bytes, 1, entry->header.a_text, outdesc);
  4304.  
  4305.   /* Free the space for the text segment.  */
  4306.   free (bytes);
  4307. }
  4308.  
  4309. /* Relocate the data segment of each input file
  4310.    and write to the output file.  */
  4311.  
  4312. void
  4313. write_data ()
  4314. {
  4315.   if (trace_files)
  4316.     fprintf (stderr, "Copying and relocating data:\n\n");
  4317.  
  4318.   each_full_file (copy_data, 0);
  4319.   file_close ();
  4320.  
  4321.   if (trace_files)
  4322.     fprintf (stderr, "\n");
  4323.  
  4324. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4325.   padfile (data_pad, outdesc);
  4326. #endif
  4327. }
  4328.  
  4329. /* Read the data segment contents of ENTRY, relocate them,
  4330.    and write the result to the output file.
  4331.    If `-r', save the data relocation for later reuse.
  4332.    See comments in `copy_text'.  */
  4333.  
  4334. void
  4335. copy_data (entry)
  4336.      struct file_entry *entry;
  4337. {
  4338.   register struct relocation_info *reloc;
  4339.   register char *bytes;
  4340.   register FILE *desc;
  4341.  
  4342.   if (trace_files)
  4343.     prline_file_name (entry, stderr);
  4344.  
  4345.   desc = file_open (entry);
  4346.  
  4347.   bytes = (char *) xmalloc (entry->header.a_data);
  4348.  
  4349.   if (entry->datarel) reloc = entry->datarel;
  4350. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4351.   else if (1)
  4352. #else
  4353.   else if (relocatable_output)    /* Will need this again */
  4354. #endif
  4355.     {
  4356.       read_file_relocation (entry);
  4357.       reloc = entry->datarel;
  4358.     }
  4359.   else
  4360.     {
  4361.       reloc = (struct relocation_info *) alloca (entry->header.a_drsize);
  4362.       fseek (desc, text_offset (entry) + entry->header.a_text
  4363.          + entry->header.a_data + entry->header.a_trsize,
  4364.          0);
  4365.       if (entry->header.a_drsize != fread (reloc, 1, entry->header.a_drsize, desc))
  4366.     fatal_with_file ("premature eof in data relocation of ", entry);
  4367.     }
  4368.  
  4369.   fseek (desc, text_offset (entry) + entry->header.a_text, 0);
  4370.   if (entry->header.a_data != fread (bytes, 1, entry->header.a_data, desc))
  4371.     fatal_with_file ("premature eof in data section of ", entry);
  4372.  
  4373.   perform_relocation (bytes, entry->data_start_address - entry->header.a_text,
  4374.               entry->header.a_data, reloc, entry->header.a_drsize,
  4375.               entry, 0);
  4376.  
  4377.   mywrite (bytes, 1, entry->header.a_data, outdesc);
  4378.  
  4379.   free (bytes);
  4380. }
  4381.  
  4382. static symbol *
  4383. retsym (p, entry)
  4384.      struct relocation_info *p;
  4385.      struct file_entry *entry;
  4386. {
  4387.   register int symindex;
  4388.   register symbol *sp;
  4389.  
  4390.   symindex = RELOC_SYMBOL (p) * sizeof (struct nlist);
  4391.  
  4392.   if (symindex >= entry->header.a_syms)
  4393.     fatal_with_file ("relocation symbolnum out of range in ", entry);
  4394.  
  4395.   sp = ((symbol *) (((struct nlist *) (((char *)entry->symbols) + symindex))
  4396.     ->n_un.n_name));
  4397.  
  4398. #ifdef N_INDR
  4399.   /* Resolve indirection */
  4400.   if ((sp->defined & ~N_EXT) == N_INDR)
  4401.     sp = (symbol *) sp->value;
  4402. #endif
  4403.   return sp;
  4404. }
  4405.  
  4406. static char *
  4407. retsymname (p, entry, addr)
  4408.     struct relocation_info *p;
  4409.     struct file_entry *entry;
  4410.     int addr;
  4411. {
  4412.   register struct nlist *n;
  4413.  
  4414.   if (RELOC_EXTERN_P(p)) {
  4415.     register symbol *sp = retsym(p, entry);
  4416.     return sp->name;
  4417.   }
  4418.   for (n=entry->symbols;
  4419.     n<(struct nlist *)((long)entry->symbols + entry->header.a_syms); n++)
  4420.     if (n->n_value == addr)
  4421.     return(entry->strings + n->n_un.n_strx);
  4422.   return "(static symbol)";
  4423. }
  4424.  
  4425. /* Relocate ENTRY's text or data section contents.
  4426.    DATA is the address of the contents, in core.
  4427.    DATA_SIZE is the length of the contents.
  4428.    PC_RELOCATION is the difference between the address of the contents
  4429.      in the output file and its address in the input file.
  4430.    RELOC_INFO is the address of the relocation info, in core.
  4431.    RELOC_SIZE is its length in bytes.  */
  4432. /* This version is about to be severley hacked by Randy.  Hope it
  4433.    works afterwards. */
  4434. void
  4435. perform_relocation (data, pc_relocation, data_size, reloc_info, reloc_size,
  4436.     entry, pure_text)
  4437.      char *data;
  4438.      struct relocation_info *reloc_info;
  4439.      struct file_entry *entry;
  4440.      int pc_relocation;
  4441.      int data_size;
  4442.      int reloc_size;
  4443.      int pure_text;
  4444. {
  4445.   register struct relocation_info *p = reloc_info;
  4446.   struct relocation_info *end
  4447.     = reloc_info + reloc_size / sizeof (struct relocation_info);
  4448.   int text_relocation = entry->text_start_address;
  4449.   int data_relocation = entry->data_start_address - entry->header.a_text;
  4450.   int bss_relocation
  4451.     = entry->bss_start_address - entry->header.a_text - entry->header.a_data;
  4452.   int text_overflow = entry->header.a_text & ~65535;
  4453.  
  4454.   for (; p < end; p++)
  4455.     {
  4456.       register int relocation = 0;
  4457.       register int addr;
  4458.       register unsigned int mask = 0;
  4459.       int oldreloc;
  4460.  
  4461. #ifdef BYTE_SWAP    /* swap relocation info */
  4462. { /* BITFIELD ALERT BITFIELD ALERT */
  4463. char *p1,*p2,p3;
  4464. p->r_address = SWAP4(p->r_address);
  4465. /* terrible hack to fix bit-fields !!! */
  4466. p1 = (char *)&p->r_address + 4;
  4467. p2 = p1+2;
  4468. p3= *p1; *p1= *p2; *p2=p3;    /* adjust r_symbolnum */
  4469. p1+= 3;                /* adjust everything else */
  4470. *p1 = ((*p1&0xf0)>>4)+((*p1&0x0f)<<4);    /* swap padding into correct spot */
  4471. *p1 = (*p1&0x6)+((*p1&0x8)>>3)+((*p1&0x1)<<3);    /* adjust other field */
  4472. }
  4473. #endif /* BYTE_SWAP */
  4474.  
  4475.       addr = RELOC_ADDRESS(p);
  4476.  
  4477.       if (addr >= data_size)
  4478.     fatal_with_file ("relocation address out of range in ", entry);
  4479.  
  4480.       if (RELOC_EXTERN_P(p))
  4481.     {
  4482.             register symbol *sp;
  4483.       sp = retsym(p, entry);
  4484.  
  4485.       /* If the symbol is undefined, leave it at zero.  */
  4486.       if (! sp->defined)
  4487.         relocation = 0;
  4488.       else
  4489.         relocation = sp->value;
  4490.     }
  4491.       else switch (RELOC_TYPE(p))
  4492.     {
  4493.     case N_TEXT:
  4494.     case N_TEXT | N_EXT:
  4495.       relocation = text_relocation;
  4496.       break;
  4497.  
  4498.     case N_DATA:
  4499.     case N_DATA | N_EXT:
  4500.       /* A word that points to beginning of the the data section
  4501.          initially contains not 0 but rather the "address" of that section
  4502.          in the input file, which is the length of the file's text.  */
  4503.       relocation = data_relocation;
  4504.       break;
  4505.  
  4506.     case N_BSS:
  4507.     case N_BSS | N_EXT:
  4508.       /* Similarly, an input word pointing to the beginning of the bss
  4509.          initially contains the length of text plus data of the file.  */
  4510.       relocation = bss_relocation;
  4511.       break;
  4512.  
  4513.     case N_ABS:
  4514.     case N_ABS | N_EXT:
  4515.       /* Don't know why this code would occur, but apparently it does.  */
  4516.       break;
  4517.  
  4518.     default:
  4519.       fatal_with_file ("nonexternal relocation code invalid in ", entry);
  4520.     }
  4521.  
  4522.       if (RELOC_PCREL_P(p))
  4523.     relocation -= pc_relocation;
  4524.  
  4525.       oldreloc = relocation;
  4526. #ifdef RELOC_ADD_EXTRA
  4527.       relocation += RELOC_ADD_EXTRA(p);
  4528. #endif
  4529.  
  4530.       relocation >>= RELOC_VALUE_RIGHTSHIFT(p);
  4531.  
  4532.       /* Unshifted mask for relocation */
  4533.       mask = 1 << (RELOC_TARGET_BITSIZE(p) - 1);
  4534.       mask |= mask - 1;
  4535.       relocation &= mask;
  4536.  
  4537.       /* Shift everything up to where it's going to be used */
  4538.       relocation <<= RELOC_TARGET_BITPOS(p);
  4539.       mask <<= RELOC_TARGET_BITPOS(p);
  4540.  
  4541.       switch (RELOC_TARGET_SIZE(p))
  4542.     {
  4543.     case 0:
  4544.       if (RELOC_MEMORY_ADD_P(p))
  4545.         relocation += mask & *(char *) (data + addr);
  4546.       *(char *) (data + addr) &= ~mask;
  4547.       *(char *) (data + addr) |= relocation;
  4548.       break;
  4549.  
  4550.     case 1:
  4551.     {
  4552.       register int word;
  4553.       if (RELOC_MEMORY_ADD_P(p)) {
  4554.         if (RELOC_PCREL_P(p)||RELOC_EXTERN_P(p))
  4555.           word = *(short *) (data + addr);
  4556.         else
  4557.           word = *(unsigned short *) (data + addr) + text_overflow;
  4558. #ifdef BYTE_SWAP
  4559.         word = SWAP2(word);
  4560. #endif
  4561.         relocation += mask & word;
  4562.       }
  4563.       if (relocation > 32767 || relocation < -32768) {
  4564.         if (!RELOC_EXTERN_P(p))
  4565.           if (!entry->strings) {
  4566.         entry->strings = (char *) xmalloc (entry->string_size);
  4567.         read_entry_strings (file_open (entry), entry);
  4568.           }
  4569.         error ("fatal error: relocation out of range (%d) for %s in %s",
  4570.         relocation, retsymname(p, entry, oldreloc+word), entry->filename);
  4571.         make_executable = 0;
  4572.       }
  4573.       word &= ~mask;
  4574.       word |= relocation;
  4575. #ifdef BYTE_SWAP
  4576.       word = SWAP2(word);
  4577. #endif
  4578.       *(short *) (data + addr) = word;
  4579.     }
  4580.       break;
  4581.  
  4582.     case 2:
  4583.     {
  4584.       register unsigned long word;
  4585. #ifndef WORD_ALIGNED
  4586.       word = *(long *) (data + addr);
  4587. #ifdef BYTE_SWAP
  4588.       word = SWAP4(word);
  4589. #endif /* BYTE_SWAP */
  4590. #else
  4591.       word = ((unsigned long)(unsigned char)(data[addr])) << 24;
  4592.       word |= ((unsigned long)(unsigned char)(data[addr+1])) << 16;
  4593.       word |= ((unsigned long)(unsigned char)(data[addr+2])) << 8;
  4594.       word |= ((unsigned long)(unsigned char)(data[addr+3]));
  4595. #ifdef BYTE_SWAP
  4596.     /* NOTE: We dont need to swap 4 because we are constructing
  4597.        the word here */
  4598. #endif
  4599.  
  4600. #endif
  4601.  
  4602.       if (RELOC_MEMORY_ADD_P(p))
  4603.         relocation += mask & word;
  4604.  
  4605.       if (pure_text && relocation > text_size+text_start) {
  4606.         if (!RELOC_EXTERN_P(p))
  4607.           if (!entry->strings) {
  4608.         entry->strings = (char *) xmalloc (entry->string_size);
  4609.         read_entry_strings (file_open (entry), entry);
  4610.           }
  4611.         error ("absolute reference to %s from pure-text in %s",
  4612.         retsymname(p, entry, relocation), entry->filename);
  4613.         make_executable = 0;    /* set exit code */
  4614.       }
  4615.       word &= ~mask;
  4616.       word |= relocation;
  4617. #if (defined(BYTE_SWAP) && (!defined(WORD_ALIGNED)))
  4618.       word = SWAP4(word);
  4619. #endif
  4620. #ifndef WORD_ALIGNED
  4621.       *(long *) (data + addr) = word;
  4622. #else
  4623.       data[addr+3] = word;
  4624.       word >>= 8;
  4625.       data[addr+2] = word;
  4626.       word >>= 8;
  4627.       data[addr+1] = word;
  4628.       word >>= 8;
  4629.       data[addr] = word;
  4630. #endif
  4631.     }
  4632.       break;
  4633.  
  4634.     default:
  4635.       fatal_with_file ("Unimplemented relocation field length in ", entry);
  4636.     }
  4637. #if 0 /*BYTE_SWAP*/        /* swap relocation info */
  4638. { /* BITFIELD ALERT BITFIELD ALERT */    /* switch it back */
  4639. char *p1,*p2,p3;
  4640. p->r_address = SWAP4(p->r_address);
  4641. /* terrible hack to fix bit-fields !!! */
  4642. p1 = (char *)&p->r_address + 4;
  4643. p2 = p1+2;
  4644. p3= *p1; *p1= *p2; *p2=p3;      /* adjust r_symbolnum */
  4645. p1+= 3;                         /* adjust everything else */
  4646. *p1 = ((*p1&0xf0)>>4)+((*p1&0x0f)<<4);  /* swap padding into correct spot */
  4647. *p1 = (*p1&0x6)+((*p1&0x8)>>3)+((*p1&0x1)<<3);  /* adjust other fields */
  4648. }
  4649. #endif /* BYTE_SWAP */
  4650.     }
  4651.     if (entry->strings)
  4652.       free (entry->strings);
  4653.     entry->strings = 0;
  4654. }
  4655.  
  4656. /* For relocatable_output only: write out the relocation,
  4657.    relocating the addresses-to-be-relocated.  */
  4658.  
  4659.  
  4660. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4661. void emit_atari_text_relocs (), emit_atari_data_relocs ();
  4662. void emit_atari_set_vectors();
  4663.  
  4664. void flush_atari_rbuf();
  4665. void emit_atari_reloc_byte();
  4666. #else
  4667. void coptxtrel (), copdatrel ();
  4668. #endif
  4669.  
  4670. void
  4671. write_rel ()
  4672. {
  4673.   register int i;
  4674.   register int count = 0;
  4675.  
  4676.   if (trace_files)
  4677.     fprintf (stderr, "Writing text relocation:\n\n");
  4678.  
  4679.   /* Assign each global symbol a sequence number, giving the order
  4680.      in which `write_syms' will write it.
  4681.      This is so we can store the proper symbolnum fields
  4682.      in relocation entries we write.  */
  4683.  
  4684.   for (i = 0; i < TABSIZE; i++)
  4685.     {
  4686.       symbol *sp;
  4687.       for (sp = symtab[i]; sp; sp = sp->link)
  4688.     if (sp->referenced || sp->defined)
  4689.       sp->def_count = count++;
  4690.     }
  4691.   if (count != defined_global_sym_count + undefined_global_sym_count)
  4692.     fatal ("internal error", 0);
  4693.  
  4694.   /* Write out the relocations of all files, remembered from copy_text.  */
  4695.  
  4696. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4697.   each_full_file (coptxtrel, 0);
  4698. #else
  4699.   each_full_file (emit_atari_text_relocs, 0);
  4700.   emit_atari_set_vectors();
  4701. #endif
  4702.  
  4703.   if (trace_files)
  4704.     fprintf (stderr, "\nWriting data relocation:\n\n");
  4705.  
  4706. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  4707.   each_full_file (copdatrel, 0);
  4708. #else
  4709.   each_full_file (emit_atari_data_relocs, 0);
  4710.   if (rbuf_last_pc == -1)
  4711.   {
  4712.       emit_atari_reloc_byte(0);
  4713.       emit_atari_reloc_byte(0);
  4714.       emit_atari_reloc_byte(0);
  4715.   }
  4716.   emit_atari_reloc_byte(0);        /* end of relocs */
  4717.   flush_atari_rbuf();
  4718. #endif
  4719.  
  4720.   if (trace_files)
  4721.     fprintf (stderr, "\n");
  4722. }
  4723.  
  4724. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4725. /* code here for dumping atari style reloc info */
  4726.  
  4727. void flush_atari_rbuf()
  4728. {
  4729. /* fprintf(stderr, "flush\n"); */
  4730.   if(rbuf_size > 0)
  4731.     fwrite(rbuf, 1, rbuf_size, outdesc);
  4732.   rbuf_size = 0;
  4733. }
  4734.  
  4735. void emit_atari_reloc_byte(b)
  4736. unsigned int b;
  4737. {
  4738.     unsigned char byte = b & 0xFF;
  4739.  
  4740. /*fprintf(stderr, "  %X", byte);*/
  4741.   if (rbuf_size >= RBUF_SIZE)
  4742.     flush_atari_rbuf();
  4743.   rbuf[rbuf_size++] = byte;
  4744. }
  4745.  
  4746. int emit_atari_reloc(rel_pc)
  4747. long rel_pc;
  4748. {
  4749.   long diff;
  4750.  
  4751. /* fprintf(stderr, "reloc at %X\n", rel_pc);  */
  4752.   if (rbuf_last_pc == -1)        /* first time thru? */
  4753.     {
  4754.     emit_atari_reloc_byte(rel_pc >> 24);
  4755.     emit_atari_reloc_byte(rel_pc >> 16);
  4756.     emit_atari_reloc_byte(rel_pc >> 8);
  4757.     emit_atari_reloc_byte(rel_pc);
  4758.     }
  4759.     else
  4760.     {
  4761.     if (rel_pc <= rbuf_last_pc)
  4762.         {
  4763.         fprintf(stderr, "Relocs out of order; last = %X curr = %X\n",
  4764.                 rbuf_last_pc, rel_pc);
  4765. /*        exit(-999);    */
  4766.         return(0);
  4767.         }
  4768.     diff = rel_pc - rbuf_last_pc;
  4769.     while (diff > 254)
  4770.         {
  4771.         emit_atari_reloc_byte(1);
  4772.         diff -= 254;
  4773.         }
  4774.     emit_atari_reloc_byte(diff);
  4775.     }
  4776.   rbuf_last_pc = rel_pc;
  4777.   return(1);
  4778. }
  4779.  
  4780. int reloc_less_p(reloc_1, reloc_2)
  4781. struct relocation_info * reloc_1, * reloc_2;
  4782. {
  4783. #if 0
  4784.   return(reloc_1->r_address < reloc_2->r_address);
  4785. #else
  4786.   return(reloc_2->r_address - reloc_1->r_address);
  4787. #endif
  4788. }
  4789.  
  4790. void emit_atari_text_relocs(entry)
  4791.      struct file_entry *entry;
  4792. {
  4793.   int reloc_num;
  4794.   int n_relocs = entry->header.a_trsize / sizeof (struct relocation_info);
  4795.   long text_base = entry->text_start_address;
  4796.  
  4797.   qsort(entry->textrel, n_relocs, sizeof (struct relocation_info), 
  4798.     reloc_less_p);
  4799.   for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
  4800.     if ((entry->textrel[reloc_num].r_length == 2) &&
  4801.         !entry->textrel[reloc_num].r_pcrel &&
  4802.       !emit_atari_reloc(entry->textrel[reloc_num].r_address + text_base))
  4803.         fprintf(stderr, "  while processing text rel %d/%d of entry %s\n",
  4804.         reloc_num, n_relocs, entry->filename);
  4805. }
  4806.  
  4807. void emit_atari_data_relocs(entry)
  4808.      struct file_entry *entry;
  4809. {
  4810.   int reloc_num;
  4811.   int n_relocs = entry->header.a_drsize / sizeof (struct relocation_info);
  4812.   long data_base = entry->data_start_address;
  4813.  
  4814.   qsort(entry->datarel, n_relocs, sizeof (struct relocation_info), 
  4815.     reloc_less_p);
  4816.   for (reloc_num = n_relocs - 1 ; reloc_num >= 0 ; reloc_num-- )
  4817.     if (entry->datarel[reloc_num].r_length == 2
  4818.     && !entry->datarel[reloc_num].r_pcrel)
  4819.     if (!emit_atari_reloc(entry->datarel[reloc_num].r_address + data_base))
  4820.         fprintf(stderr, "  while processing data rel %d/%d of entry %s\n",
  4821.         reloc_num, n_relocs, entry->filename);
  4822. }
  4823.  
  4824.  
  4825. /* Handle set vectors. This kludge should only be necessary for C++.
  4826.  * Skip through the set vectors, outputting relocation for each vector.
  4827.  */
  4828. void emit_atari_set_vectors()
  4829. {
  4830.   unsigned long *current_element = set_vectors;
  4831.   int num_vectors = 0, loop;
  4832.   long num_elements = 0;
  4833.   
  4834.   if (set_sect_size != 0) {
  4835.  
  4836.     current_element = set_vectors;
  4837.     while (num_elements < (set_sect_size / sizeof(unsigned long))) {
  4838.       num_vectors = *current_element++; /* Number of vectors for current sym */
  4839.       num_elements++;
  4840.       for (loop = 1; loop <= num_vectors; loop++) {
  4841.         emit_atari_reloc(set_sect_start + num_elements++ * sizeof (unsigned long));
  4842.         current_element++;
  4843.       }
  4844.       current_element++; num_elements++;  /* There's a zero at the end. */
  4845.     }
  4846.   }
  4847. }
  4848.  
  4849. #endif
  4850.  
  4851. #if (!((defined(CROSSATARI) || defined(atarist) || defined(atariminix))))
  4852. void
  4853. coptxtrel (entry)
  4854.      struct file_entry *entry;
  4855. {
  4856.   register struct relocation_info *p, *end;
  4857.   register int reloc = entry->text_start_address;
  4858.  
  4859.   p = entry->textrel;
  4860.   end = (struct relocation_info *) (entry->header.a_trsize + (char *) p);
  4861.   while (p < end)
  4862.     {
  4863.       RELOC_ADDRESS(p) += reloc;
  4864.       if (RELOC_EXTERN_P(p))
  4865.     {
  4866.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4867.       symbol *symptr = ((symbol *)
  4868.                 (((struct nlist *)
  4869.                   (((char *)entry->symbols) + symindex))
  4870.                  ->n_un.n_name));
  4871.  
  4872.       if (symindex >= entry->header.a_syms)
  4873.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4874.  
  4875.       /* If the symbol is now defined, change the external relocation
  4876.          to an internal one.  */
  4877.  
  4878.       if (symptr->defined)
  4879.         {
  4880.           RELOC_EXTERN_P(p) = 0;
  4881.           RELOC_SYMBOL(p) = (symptr->defined & ~N_EXT);
  4882.         }
  4883.       else
  4884.           RELOC_SYMBOL(p) = (symptr->def_count + nsyms
  4885.                  - defined_global_sym_count
  4886.                  - undefined_global_sym_count);
  4887.     }
  4888.       p++;
  4889.     }
  4890.   mywrite (entry->textrel, 1, entry->header.a_trsize, outdesc);
  4891. }
  4892.  
  4893. void
  4894. copdatrel (entry)
  4895.      struct file_entry *entry;
  4896. {
  4897.   register struct relocation_info *p, *end;
  4898.   /* Relocate the address of the relocation.
  4899.      Old address is relative to start of the input file's data section.
  4900.      New address is relative to start of the output file's data section.  */
  4901.   register int reloc = entry->data_start_address - text_size;
  4902.  
  4903.   p = entry->datarel;
  4904.   end = (struct relocation_info *) (entry->header.a_drsize + (char *) p);
  4905.   while (p < end)
  4906.     {
  4907.       RELOC_ADDRESS(p) += reloc;
  4908.       if (RELOC_EXTERN_P(p))
  4909.     {
  4910.       register int symindex = RELOC_SYMBOL(p) * sizeof (struct nlist);
  4911.       symbol *symptr = ((symbol *)
  4912.                 (((struct nlist *)
  4913.                   (((char *)entry->symbols) + symindex))
  4914.                  ->n_un.n_name));
  4915.       int symtype = symptr->defined & ~N_EXT;
  4916.  
  4917.       if (symindex >= entry->header.a_syms)
  4918.         fatal_with_file ("relocation symbolnum out of range in ", entry);
  4919.       if (force_common_definition
  4920.           || symtype == N_DATA || symtype == N_TEXT || symtype == N_ABS)
  4921.         {
  4922.           RELOC_EXTERN_P(p) = 0;
  4923.           RELOC_SYMBOL(p) = symtype;
  4924.         }
  4925.       else
  4926.         RELOC_SYMBOL(p)
  4927.           = (((symbol *)
  4928.           (((struct nlist *)
  4929.             (((char *)entry->symbols) + symindex))
  4930.            ->n_un.n_name))
  4931.          ->def_count
  4932.          + nsyms - defined_global_sym_count
  4933.          - undefined_global_sym_count);
  4934.     }
  4935.       p++;
  4936.     }
  4937.   mywrite (entry->datarel, 1, entry->header.a_drsize, outdesc);
  4938. }
  4939. #endif /* ! atari */
  4940.  
  4941. void write_file_syms ();
  4942. void write_string_table ();
  4943.  
  4944.  
  4945. #if (defined(CROSSATARI) || defined(atarist) || defined(atariminix))
  4946. /* this is all different on ataris */
  4947.  
  4948. #if (!(defined(MINIX) || defined(atariminix)))
  4949.  
  4950. /* atari TOS version */
  4951.  
  4952. #define N_ATARI_FILENAME 0x1E
  4953.  
  4954. void write_atari_sym(p, str)
  4955. register struct nlist * p;
  4956. char * str;
  4957. {
  4958.   struct asym sym;
  4959.   int i;
  4960.   char *ptr;
  4961.  
  4962.   /* fprintf(stderr, "sym %2ld %s\n", strlen(str), str); */
  4963.  
  4964.   switch (p->n_type & N_TYPE)
  4965.     {
  4966.     case N_UNDF: sym.a_type = A_UNDF; break;
  4967.     case N_ABS:  sym.a_type = A_EQU; break; /* ??? */
  4968.         case N_SETT:
  4969.         case N_SETV:
  4970.     case N_TEXT: sym.a_type = A_TEXT; break;
  4971.         case N_SETD:
  4972.     case N_DATA: sym.a_type = A_DATA; break;
  4973.         case N_SETB:
  4974.     case N_BSS:  sym.a_type = A_BSS; break;
  4975.         case N_ATARI_FILENAME: sym.a_type = A_TFILE; break;
  4976.     default:     sym.a_type = A_UNDF; break;
  4977.  
  4978. /* figure out rest of this later */
  4979.     }
  4980.   if (p->n_type & N_EXT) /* differenciate between local/global syms */
  4981.     sym.a_type |= A_GLOBL;
  4982.   ptr = &(sym.a_name[0]);
  4983.   i = sizeof(sym.a_name);
  4984.   while (--i >= 0 && ('\0' != (*ptr++ = *str)))
  4985.     str++;
  4986.  
  4987.   /* if i >= 0 then *str == '\0' and if i == 0 there is nothing to fill */
  4988.   if (i > 0)                /* we are done - fill it with 0 */
  4989.     {
  4990.       do {
  4991.     *ptr++ = '\0';
  4992.       } while (--i > 0);
  4993.     }
  4994.   else if (*str && gst_symboltable)    /* if more to write */
  4995.     {
  4996.       sym.a_type |= A_LNAM;
  4997. #ifdef BYTE_SWAP
  4998.       sym.a_type = SWAP2(sym.a_type);
  4999. #endif
  5000.       i = sizeof sym;
  5001.       gst_symbols++;
  5002.     }
  5003. fake_out:
  5004.   sym.a_value = p->n_value;
  5005.  
  5006. #ifndef WORD_ALIGNED
  5007.   mywrite((char *)&sym, sizeof sym, 1, outdesc);
  5008.   if (i > 0)
  5009.     {
  5010.       (void) strncpy ((char *) &sym, str, (size_t)i);
  5011.       mywrite((char *)&sym, sizeof sym, 1, outdesc);
  5012.     }
  5013. #else
  5014.   mywrite(sym.a_name , sizeof sym.a_name , 1, outdesc);
  5015.   mywrite(&sym.a_type , sizeof sym.a_type , 1, outdesc);
  5016.   mywrite(&sym.a_value, sizeof sym.a_value, 1, outdesc);
  5017.   if (i > 0)
  5018.     {
  5019.       (void) strncpy ((char *) &sym, str, (size_t)i);
  5020.       mywrite(&sym, 14, 1, outdesc);
  5021.     }
  5022. #endif
  5023. }
  5024.  
  5025. void write_atari_syms(entry, syms_written_addr)
  5026. struct file_entry * entry;
  5027. int * syms_written_addr;
  5028. {
  5029.   register struct nlist *p = entry->symbols;
  5030.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  5031. /*   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;  */
  5032.  
  5033.   /* Read the file's string table.  */
  5034.  
  5035.   entry->strings = (char *) xmalloc (entry->string_size);
  5036.   read_entry_strings (file_open (entry), entry);
  5037.  
  5038.   /* Generate a local symbol for the start of this file's text.  */
  5039.  
  5040.   if (discard_locals != DISCARD_ALL)
  5041.     {
  5042.       struct nlist nl;
  5043.  
  5044.       nl.n_type = N_ATARI_FILENAME;
  5045. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  5046.       nl.n_value = entry->text_start_address;
  5047.       nl.n_desc = 0;
  5048.       nl.n_other = 0;
  5049. /*      *bufp++ = nl;    */
  5050.       write_atari_sym(&nl, entry->local_sym_name);
  5051.       (*syms_written_addr)++;
  5052. /* necessary? */
  5053.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  5054.     }
  5055.  
  5056.   for (; p < end; p++)
  5057.     {
  5058.       register int type = p->n_type;
  5059.       register int write = 0;
  5060.  
  5061.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  5062.  
  5063.       if (SET_ELEMENT_P (type)) /* This occurs even if global.  These */
  5064.                 /* types of symbols are never written */
  5065.                 /* globally, though they are stored */
  5066.                 /* globally.  */
  5067.         write = 1;
  5068.       else if (!(type & (N_STAB | N_EXT)))
  5069.         /* ordinary local symbol */
  5070.     write = (discard_locals != DISCARD_ALL)
  5071.         && !(discard_locals == DISCARD_L &&
  5072.              (p->n_un.n_strx + entry->strings)[0] == 'L');
  5073.       else if (!(type & N_EXT))
  5074.     /* debugger symbol */
  5075.         write = (strip_symbols == STRIP_NONE);
  5076.  
  5077.       if (write)
  5078.     {
  5079.       /* If this symbol has a name, write it */
  5080.  
  5081.       if (p->n_un.n_strx)
  5082.         write_atari_sym(p, (SET_ELEMENT_P (type)
  5083.                 ? ((symbol *) p->n_un.n_name)->name
  5084.                 : p->n_un.n_strx + entry->strings));
  5085.       else
  5086.         write_atari_sym (p, "DBXSYM");
  5087.       (*syms_written_addr)++;
  5088.     }
  5089.     }
  5090.   free (entry->strings);
  5091.   entry->strings = NULL;  
  5092. }
  5093.  
  5094. void write_syms ()
  5095. {
  5096.   int n_syms_written = 0;
  5097.   register symbol *sp;
  5098.   int i;
  5099.  
  5100.   if (strip_symbols == STRIP_ALL)
  5101.     return;
  5102.  
  5103.   each_file(write_atari_syms, &n_syms_written);
  5104.   file_close ();
  5105.  
  5106.   /* Now write out the global symbols.  */
  5107.  
  5108.   /* Scan the symbol hash table, bucket by bucket.  */
  5109.  
  5110.   for (i = 0; i < TABSIZE; i++)
  5111.     for (sp = symtab[i]; sp; sp = sp->link)
  5112.       {
  5113.     struct nlist nl;
  5114.  
  5115.     nl.n_other = 0;
  5116.     nl.n_desc = 0;
  5117.  
  5118.     /* Compute a `struct nlist' for the symbol.  */
  5119.  
  5120.     if (sp->defined || sp->referenced)
  5121.       {
  5122.         if (!sp->defined)          /* undefined -- legit only if -r */
  5123.           {
  5124.         nl.n_type = N_UNDF | N_EXT;
  5125.         nl.n_value = 0;
  5126.           }
  5127.         else if (sp->defined > 1) /* defined with known type */
  5128.           {
  5129.         /* If the target of an indirect symbol has been
  5130.            defined and we are outputting an executable,
  5131.            resolve the indirection; it's no longer needed */
  5132.         if (!relocatable_output
  5133.             && ((sp->defined & ~N_EXT) == N_INDR)
  5134.             && (((symbol *) sp->value)->defined > 1))
  5135.           {
  5136.             symbol *newsp = (symbol *) sp->value;
  5137.             nl.n_type = newsp->defined;
  5138.             nl.n_value = newsp->value;
  5139.           }
  5140.         else
  5141.           {
  5142.             nl.n_type = sp->defined;
  5143.             if (sp->defined != (N_INDR | N_EXT))
  5144.               nl.n_value = sp->value;
  5145.             else
  5146.               nl.n_value = 0;
  5147.           }
  5148.           }
  5149.         else if (sp->max_common_size) /* defined as common but not allocated. */
  5150.           {
  5151.         /* happens only with -r and not -d */
  5152.         /* write out a common definition */
  5153.         nl.n_type = N_UNDF | N_EXT;
  5154.         nl.n_value = sp->max_common_size;
  5155.           }
  5156.         else
  5157.           fatal ("internal error: %s defined in mysterious way", sp->name);
  5158.  
  5159.         /* write and count it.  */
  5160. #ifdef BYTE_SWAP
  5161. nl.n_un.n_strx    = SWAP4(nl.n_un.n_strx);
  5162. nl.n_desc      = SWAP2(nl.n_desc);
  5163. nl.n_value      = SWAP4(nl.n_value);
  5164. #endif
  5165.         write_atari_sym(&nl, sp->name);
  5166.         n_syms_written++;
  5167.       }
  5168.       }
  5169.  
  5170.   if (n_syms_written != nsyms)
  5171.     fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  5172.              n_syms_written, nsyms);
  5173.   
  5174.       
  5175. }
  5176.  
  5177. #else /* Atari Minix */
  5178.  
  5179. void write_atari_sym(p, str, gflag)
  5180. register struct nlist * p;
  5181. char * str;
  5182. int gflag;
  5183. {
  5184.   struct asym sym;
  5185.   int i;
  5186.  
  5187. /* fprintf(stderr, "sym %s\n", str); */
  5188.  
  5189.   for (i = 0 ; ((i < 8) && str[i]) ; i++)
  5190.     sym.a_name[i] = str[i];
  5191.   for ( ; i < 8 ; i++)
  5192.     sym.a_name[i] = ' ';
  5193.   switch (p->n_type & N_TYPE)
  5194.     {
  5195.     case N_UNDF: sym.a_sclass = A_UNDF; break;
  5196.     case N_ABS:  sym.a_sclass = A_ABS; break;
  5197.     case N_TEXT: sym.a_sclass = A_TEXT; break;
  5198.     case N_DATA: sym.a_sclass = A_DATA; break;
  5199.     case N_BSS:  sym.a_sclass = A_BSS; break;
  5200.     default:     sym.a_sclass = 0;
  5201.     }
  5202.   if(gflag)
  5203.       sym.a_sclass |= A_EXT;
  5204.  
  5205.   sym.a_value = p->n_value;
  5206.   sym.a_numaux = sym.a_type = 0;
  5207. #ifndef WORD_ALIGNED
  5208.   mywrite(&sym, sizeof sym, 1, outdesc);
  5209. #else
  5210.   mywrite(&sym.a_name , sizeof sym.a_name , 1, outdesc);
  5211.   mywrite(&sym.a_value, sizeof sym.a_value, 1, outdesc);
  5212.   mywrite(&sym.a_sclass, sizeof sym.a_sclass, 1, outdesc);
  5213.   mywrite(&sym.a_numaux , sizeof sym.a_numaux , 1, outdesc);
  5214.   mywrite(&sym.a_type , sizeof sym.a_type , 1, outdesc);
  5215. #endif
  5216.  
  5217. }
  5218.  
  5219. void write_atari_syms(entry, syms_written_addr)
  5220. struct file_entry * entry;
  5221. int * syms_written_addr;
  5222. {
  5223.   register struct nlist *p = entry->symbols;
  5224.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  5225.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  5226.  
  5227.   /* Read the file's string table.  */
  5228.  
  5229.   entry->strings = (char *) xmalloc (entry->string_size);
  5230.   read_entry_strings (file_open (entry), entry);
  5231.  
  5232.   /* Generate a local symbol for the start of this file's text.  */
  5233.  
  5234.   if (discard_locals != DISCARD_ALL)
  5235.     {
  5236.       struct nlist nl;
  5237.  
  5238.       nl.n_type = N_TEXT;
  5239. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  5240.       nl.n_value = entry->text_start_address;
  5241.       nl.n_desc = 0;
  5242.       nl.n_other = 0;
  5243. /*      *bufp++ = nl;    */
  5244.       write_atari_sym(&nl, entry->local_sym_name, 0);
  5245.       (*syms_written_addr)++;
  5246. /* necessary? */
  5247.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  5248.     }
  5249.  
  5250.   for (; p < end; p++)
  5251.     {
  5252.       register int type = p->n_type;
  5253.       register int write = 0;
  5254.  
  5255.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  5256.  
  5257.       if (!(type & (N_STAB | N_EXT)))
  5258.         /* ordinary local symbol */
  5259.     write = (discard_locals != DISCARD_ALL)
  5260.         && !(discard_locals == DISCARD_L &&
  5261.              (p->n_un.n_strx + entry->strings)[0] == 'L');
  5262.       else if (!(type & N_EXT))
  5263.     /* debugger symbol */
  5264.       /*        write = (strip_symbols == STRIP_NONE); */ write = 0;
  5265.       
  5266.  
  5267.       if (write)
  5268.     {
  5269.       /* If this symbol has a name, write it */
  5270.  
  5271.       if (p->n_un.n_strx)
  5272.         {
  5273.         write_atari_sym(p, p->n_un.n_strx + entry->strings, 0);
  5274.         (*syms_written_addr)++;
  5275.         }
  5276.     }
  5277.     }
  5278.   free (entry->strings);
  5279.   entry->strings = NULL;  
  5280. }
  5281.  
  5282. void write_syms ()
  5283. {
  5284.   int n_syms_written = 0;
  5285.   register symbol *sp;
  5286.   int i;
  5287.  
  5288.   if (strip_symbols == STRIP_ALL)
  5289.     return;
  5290.  
  5291.   each_file(write_atari_syms, &n_syms_written);
  5292.  
  5293.   /* Now write out the global symbols.  */
  5294.  
  5295.   /* Scan the symbol hash table, bucket by bucket.  */
  5296.  
  5297.   for (i = 0; i < TABSIZE; i++)
  5298.     for (sp = symtab[i]; sp; sp = sp->link)
  5299.       {
  5300.     struct nlist nl;
  5301.  
  5302.     nl.n_other = 0;
  5303.     nl.n_desc = 0;
  5304.  
  5305.     /* Compute a `struct nlist' for the symbol.  */
  5306.  
  5307.     if (sp->defined || sp->referenced)
  5308.       {
  5309.         if (!sp->defined)          /* undefined -- legit only if -r */
  5310.           {
  5311.         nl.n_type = N_UNDF | N_EXT;
  5312.         nl.n_value = 0;
  5313.           }
  5314.         else if (sp->defined > 1) /* defined with known type */
  5315.           {
  5316.         nl.n_type = sp->defined;
  5317.         nl.n_value = sp->value;
  5318.           }
  5319.         else if (sp->max_common_size) /* defined as common but not allocated. */
  5320.           {
  5321.         /* happens only with -r and not -d */
  5322.         /* write out a common definition */
  5323.         nl.n_type = N_UNDF | N_EXT;
  5324.         nl.n_value = sp->max_common_size;
  5325.           }
  5326.         else
  5327.           fatal ("internal error: %s defined in mysterious way", sp->name);
  5328.  
  5329.         /* write and count it.  */
  5330.  
  5331.         write_atari_sym(&nl, sp->name, 1);
  5332.         n_syms_written++;
  5333.       }
  5334.       }
  5335.   if (n_syms_written != nsyms)
  5336.   {
  5337.       register long pos;
  5338.       
  5339. #if 0
  5340.       fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  5341.           n_syms_written, nsyms);
  5342. #endif
  5343.     /* go patch header */
  5344. #ifndef WORD_ALIGNED
  5345.       n_syms_written = n_syms_written * sizeof(struct asym);
  5346. #else
  5347.       n_syms_written = n_syms_written * 16;
  5348. #endif
  5349.       pos = ftell(outdesc);
  5350.       fseek(outdesc, 28L, 0);
  5351.       mywrite(&n_syms_written, 4, 1, outdesc);
  5352.       fseek(outdesc, pos, 0);
  5353.     }
  5354.   
  5355. }
  5356. #endif /* MINIX */
  5357.  
  5358. #else  /* for Gnu/Unix */
  5359.  
  5360. /* Offsets and current lengths of symbol and string tables in output file. */
  5361.  
  5362. int symbol_table_offset;
  5363. int symbol_table_len;
  5364.  
  5365. /* Address in output file where string table starts.  */
  5366. int string_table_offset;
  5367.  
  5368. /* Offset within string table
  5369.    where the strings in `strtab_vector' should be written.  */
  5370. int string_table_len;
  5371.  
  5372. /* Total size of string table strings allocated so far,
  5373.    including strings in `strtab_vector'.  */
  5374. int strtab_size;
  5375.  
  5376. /* Vector whose elements are strings to be added to the string table.  */
  5377. char **strtab_vector;
  5378.  
  5379. /* Vector whose elements are the lengths of those strings.  */
  5380. int *strtab_lens;
  5381.  
  5382. /* Index in `strtab_vector' at which the next string will be stored.  */
  5383. int strtab_index;
  5384.  
  5385. /* Add the string NAME to the output file string table.
  5386.    Record it in `strtab_vector' to be output later.
  5387.    Return the index within the string table that this string will have.  */
  5388.  
  5389. int
  5390. assign_string_table_index (name)
  5391.      char *name;
  5392. {
  5393.   register int index = strtab_size;
  5394.   register int len = strlen (name) + 1;
  5395.  
  5396.   strtab_size += len;
  5397.   strtab_vector[strtab_index] = name;
  5398.   strtab_lens[strtab_index++] = len;
  5399.  
  5400.   return index;
  5401. }
  5402.  
  5403. FILE *outstream = (FILE *) 0;
  5404.  
  5405. /* Write the contents of `strtab_vector' into the string table.
  5406.    This is done once for each file's local&debugger symbols
  5407.    and once for the global symbols.  */
  5408.  
  5409. void
  5410. write_string_table ()
  5411. {
  5412.   register int i;
  5413.  
  5414.   fseek (outdesc, string_table_offset + string_table_len, 0);
  5415.  
  5416.   if (!outstream)
  5417.     outstream = outdesc;
  5418.  
  5419.   for (i = 0; i < strtab_index; i++)
  5420.     {
  5421.       fwrite (strtab_vector[i], 1, strtab_lens[i], outstream);
  5422.       string_table_len += strtab_lens[i];
  5423.     }
  5424.  
  5425.   fflush (outstream);
  5426.  
  5427.   /* Report I/O error such as disk full.  */
  5428.   if (ferror (outstream))
  5429.     perror_name (output_filename);
  5430. }
  5431.  
  5432. /* Write the symbol table and string table of the output file.  */
  5433.  
  5434. void
  5435. write_syms ()
  5436. {
  5437.   /* Number of symbols written so far.  */
  5438.   int syms_written = 0;
  5439.   register int i;
  5440.   register symbol *sp;
  5441.  
  5442.   /* Buffer big enough for all the global symbols.  One
  5443.      extra struct for each indirect symbol to hold the extra reference
  5444.      following. */
  5445.   struct nlist *buf
  5446.     = (struct nlist *) xmalloc ((defined_global_sym_count
  5447.                  + undefined_global_sym_count
  5448.                  + global_indirect_count)
  5449.                 * sizeof (struct nlist));
  5450.   /* Pointer for storing into BUF.  */
  5451.   register struct nlist *bufp = buf;
  5452.  
  5453.   /* Size of string table includes the bytes that store the size.  */
  5454.   strtab_size = sizeof strtab_size;
  5455.  
  5456.   symbol_table_offset = N_SYMOFF (outheader);
  5457.   symbol_table_len = 0;
  5458.   string_table_offset = N_STROFF (outheader);
  5459.   string_table_len = strtab_size;
  5460.  
  5461.   if (strip_symbols == STRIP_ALL)
  5462.     {
  5463.       free (buf);
  5464.       return;
  5465.     }
  5466.  
  5467.   /* Write the local symbols defined by the various files.  */
  5468.  
  5469.   each_file (write_file_syms, &syms_written);
  5470.   file_close ();
  5471.  
  5472.   /* Now write out the global symbols.  */
  5473.  
  5474.   /* Allocate two vectors that record the data to generate the string
  5475.      table from the global symbols written so far.  This must include
  5476.      extra space for the references following indirect outputs. */
  5477.  
  5478.   strtab_vector = (char **) xmalloc ((num_hash_tab_syms
  5479.                       + global_indirect_count) * sizeof (char *));
  5480.   strtab_lens = (int *) xmalloc ((num_hash_tab_syms
  5481.                   + global_indirect_count) * sizeof (int));
  5482.   strtab_index = 0;
  5483.  
  5484.   /* Scan the symbol hash table, bucket by bucket.  */
  5485.  
  5486.   for (i = 0; i < TABSIZE; i++)
  5487.     for (sp = symtab[i]; sp; sp = sp->link)
  5488.       {
  5489.     struct nlist nl;
  5490.  
  5491.     nl.n_other = 0;
  5492.     nl.n_desc = 0;
  5493.  
  5494.     /* Compute a `struct nlist' for the symbol.  */
  5495.  
  5496.     if (sp->defined || sp->referenced)
  5497.       {
  5498.         /* common condition needs to be before undefined condition */
  5499.         /* because unallocated commons are set undefined in */
  5500.         /* digest_symbols */
  5501.         if (sp->defined > 1) /* defined with known type */
  5502.           {
  5503.         /* If the target of an indirect symbol has been
  5504.            defined and we are outputting an executable,
  5505.            resolve the indirection; it's no longer needed */
  5506.         if (!relocatable_output
  5507.             && ((sp->defined & ~N_EXT) == N_INDR)
  5508.             && (((symbol *) sp->value)->defined > 1))
  5509.           {
  5510.             symbol *newsp = (symbol *) sp->value;
  5511.             nl.n_type = newsp->defined;
  5512.             nl.n_value = newsp->value;
  5513.           }
  5514.         else
  5515.           {
  5516.             nl.n_type = sp->defined;
  5517.             if (sp->defined != (N_INDR | N_EXT))
  5518.               nl.n_value = sp->value;
  5519.             else
  5520.               nl.n_value = 0;
  5521.           }
  5522.           }
  5523.         else if (sp->max_common_size) /* defined as common but not allocated. */
  5524.           {
  5525.         /* happens only with -r and not -d */
  5526.         /* write out a common definition */
  5527.         nl.n_type = N_UNDF | N_EXT;
  5528.         nl.n_value = sp->max_common_size;
  5529.           }
  5530.         else if (!sp->defined)          /* undefined -- legit only if -r */
  5531.           {
  5532.         nl.n_type = N_UNDF | N_EXT;
  5533.         nl.n_value = 0;
  5534.           }
  5535.         else
  5536.           fatal ("internal error: %s defined in mysterious way", sp->name);
  5537.  
  5538.         /* Allocate string table space for the symbol name.  */
  5539.  
  5540.         nl.n_un.n_strx = assign_string_table_index (sp->name);
  5541.  
  5542.         /* Output to the buffer and count it.  */
  5543.  
  5544.         *bufp++ = nl;
  5545.         syms_written++;
  5546.         if (nl.n_type == (N_INDR | N_EXT))
  5547.           {
  5548.         struct nlist xtra_ref;
  5549.         xtra_ref.n_type = N_EXT | N_UNDF;
  5550.         xtra_ref.n_un.n_strx =
  5551.           assign_string_table_index (((symbol *) sp->value)->name);
  5552.         xtra_ref.n_other = 0;
  5553.         xtra_ref.n_desc = 0;
  5554.         xtra_ref.n_value = 0;
  5555.         *bufp++ = xtra_ref;
  5556.         syms_written++;
  5557.           }
  5558.       }
  5559.       }
  5560.  
  5561.   /* Output the buffer full of `struct nlist's.  */
  5562.  
  5563.   fseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  5564.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  5565.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  5566.  
  5567.   if (syms_written != nsyms)
  5568.     fatal ("internal error: wrong number of symbols written into output file", 0);
  5569.  
  5570.   if (symbol_table_offset + symbol_table_len != string_table_offset)
  5571.     fatal ("internal error: inconsistent symbol table length", 0);
  5572.  
  5573.   /* Now the total string table size is known, so write it.
  5574.      We are already positioned at the right place in the file.  */
  5575.  
  5576.   mywrite (&strtab_size, sizeof (int), 1, outdesc);  /* we're at right place */
  5577.  
  5578.   /* Write the strings for the global symbols.  */
  5579.  
  5580.   write_string_table ();
  5581.  
  5582.   /* Free the buffers.  */
  5583.  
  5584.   free (strtab_lens);
  5585.   free (strtab_vector);
  5586.   free (buf);
  5587. }
  5588.  
  5589. /* Write the local and debugger symbols of file ENTRY.
  5590.    Increment *SYMS_WRITTEN_ADDR for each symbol that is written.  */
  5591.  
  5592. /* Note that we do not combine identical names of local symbols.
  5593.    dbx or gdb would be confused if we did that.  */
  5594.  
  5595. void
  5596. write_file_syms (entry, syms_written_addr)
  5597.      struct file_entry *entry;
  5598.      int *syms_written_addr;
  5599. {
  5600.   register struct nlist *p = entry->symbols;
  5601.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  5602.  
  5603.   /* Buffer to accumulate all the syms before writing them.
  5604.      It has one extra slot for the local symbol we generate here.  */
  5605.   struct nlist *buf
  5606.     = (struct nlist *) xmalloc (entry->header.a_syms + sizeof (struct nlist));
  5607.   register struct nlist *bufp = buf;
  5608.  
  5609.   /* Upper bound on number of syms to be written here.  */
  5610.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  5611.  
  5612.   /* Make tables that record, for each symbol, its name and its name's length.
  5613.      The elements are filled in by `assign_string_table_index'.  */
  5614.  
  5615.   strtab_vector = (char **) xmalloc (max_syms * sizeof (char *));
  5616.   strtab_lens = (int *) xmalloc (max_syms * sizeof (int));
  5617.   strtab_index = 0;
  5618.  
  5619.   /* Generate a local symbol for the start of this file's text.  */
  5620.  
  5621.   if (discard_locals != DISCARD_ALL)
  5622.     {
  5623.       struct nlist nl;
  5624.  
  5625.       nl.n_type = N_TEXT;
  5626.       nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name);
  5627.       nl.n_value = entry->text_start_address;
  5628.       nl.n_desc = 0;
  5629.       nl.n_other = 0;
  5630.       *bufp++ = nl;
  5631.       (*syms_written_addr)++;
  5632.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  5633.     }
  5634.  
  5635.   /* Read the file's string table.  */
  5636.  
  5637.   entry->strings = (char *) xmalloc (entry->string_size);
  5638.   read_entry_strings (file_open (entry), entry);
  5639.  
  5640.   for (; p < end; p++)
  5641.     {
  5642.       register int type = p->n_type;
  5643.       register int write = 0;
  5644.  
  5645.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  5646.  
  5647.  
  5648.       if (SET_ELEMENT_P (type))     /* This occurs even if global.  These */
  5649.                 /* types of symbols are never written */
  5650.                 /* globally, though they are stored */
  5651.                 /* globally.  */
  5652.         write = relocatable_output;
  5653.       else if (!(type & (N_STAB | N_EXT)))
  5654.         /* ordinary local symbol */
  5655.     write = ((discard_locals != DISCARD_ALL)
  5656.          && !(discard_locals == DISCARD_L &&
  5657.               (p->n_un.n_strx + entry->strings)[0] == 'L')
  5658.          && type != N_WARNING);
  5659.       else if (!(type & N_EXT))
  5660.     /* debugger symbol */
  5661.         write = (strip_symbols == STRIP_NONE);
  5662.  
  5663.       if (write)
  5664.     {
  5665.       /* If this symbol has a name,
  5666.          allocate space for it in the output string table.  */
  5667.  
  5668. #if 0                /* Following no longer true  */
  5669.       /* Sigh.  If this is a set element, it has been entered in */
  5670.       /* the global symbol table *and* is being output as a */
  5671.       /* local.  This means that p->n_un.n_strx has been */
  5672.       /* trashed.  */
  5673.       if (SET_ELEMENT_P (type))
  5674.         p->n_un.n_strx =
  5675.           assign_string_table_index (((symbol *) p->n_un.n_name)->name);
  5676.       else
  5677. #endif
  5678.       if (p->n_un.n_strx)
  5679.         p->n_un.n_strx = assign_string_table_index (p->n_un.n_strx
  5680.                             + entry->strings);
  5681.  
  5682.       /* Output this symbol to the buffer and count it.  */
  5683.  
  5684.       *bufp++ = *p;
  5685.       (*syms_written_addr)++;
  5686.     }
  5687.     }
  5688.  
  5689.   /* All the symbols are now in BUF; write them.  */
  5690.  
  5691.   fseek (outdesc, symbol_table_offset + symbol_table_len, 0);
  5692.   mywrite (buf, sizeof (struct nlist), bufp - buf, outdesc);
  5693.   symbol_table_len += sizeof (struct nlist) * (bufp - buf);
  5694.  
  5695.   /* Write the string-table data for the symbols just written,
  5696.      using the data in vectors `strtab_vector' and `strtab_lens'.  */
  5697.  
  5698.   write_string_table ();
  5699.  
  5700.   /* Free all buffers.  */
  5701.   free (entry->strings);
  5702.   entry->strings = 0;
  5703.   free (strtab_lens);
  5704.   free (strtab_vector);
  5705.   free (buf);
  5706. }
  5707. #endif            /* not atari st */
  5708.  
  5709.  
  5710. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  5711. /* Copy any GDB symbol segments from the input files to the output file.
  5712.    The contents of the symbol segment is copied without change
  5713.    except that we store some information into the beginning of it.  */
  5714.  
  5715. void write_file_symseg ();
  5716.  
  5717. void
  5718. write_symsegs ()
  5719. {
  5720.   each_file (write_file_symseg, 0);
  5721. }
  5722.  
  5723. void
  5724. write_file_symseg (entry)
  5725.      struct file_entry *entry;
  5726. {
  5727.   char buffer[4096];
  5728.   struct symbol_root root;
  5729.   FILE *indesc;
  5730.   int len;
  5731.  
  5732.   if (entry->symseg_offset == 0)
  5733.     return;
  5734.  
  5735.   /* This entry has a symbol segment.  Read the root of the segment.  */
  5736.  
  5737.   indesc = file_open (entry);
  5738.   fseek (indesc, entry->symseg_offset + entry->starting_offset, 0);
  5739.   if (sizeof root != fread (&root, 1, sizeof root, indesc))
  5740.     fatal_with_file ("premature end of file in symbol segment of ", entry);
  5741.  
  5742.   /* Store some relocation info into the root.  */
  5743.  
  5744.   root.ldsymoff = entry->local_syms_offset;
  5745.   root.textrel = entry->text_start_address;
  5746.   root.datarel = entry->data_start_address - entry->header.a_text;
  5747.   root.bssrel = entry->bss_start_address
  5748.     - entry->header.a_text - entry->header.a_data;
  5749.   root.databeg = entry->data_start_address - root.datarel;
  5750.   root.bssbeg = entry->bss_start_address - root.bssrel;
  5751.  
  5752.   /* Write the modified root into the output file.  */
  5753.  
  5754.   mywrite (&root, sizeof root, 1, outdesc);
  5755.  
  5756.   /* Copy the rest of the symbol segment unchanged.  */
  5757.  
  5758.   if (entry->superfile)
  5759.     {
  5760.       /* Library member: number of bytes to copy is determined
  5761.      from the member's total size.  */
  5762.  
  5763.       int total = entry->total_size - entry->symseg_offset - sizeof root;
  5764.  
  5765.       while (total > 0)
  5766.     {
  5767.       len = fread (buffer, 1, min (sizeof buffer, total), indesc);
  5768.  
  5769.       if (len != min (sizeof buffer, total))
  5770.         fatal_with_file ("premature end of file in symbol segment of ", entry);
  5771.       total -= len;
  5772.       mywrite (buffer, len, 1, outdesc);
  5773.     }
  5774.     }
  5775.   else
  5776.     {
  5777.       /* A separate file: copy until end of file.  */
  5778.  
  5779.       while (len = fread (buffer, 1, sizeof buffer, indesc))
  5780.     {
  5781.       mywrite (buffer, len, 1, outdesc);
  5782.       if (len < sizeof buffer)
  5783.         break;
  5784.     }
  5785.     }
  5786.  
  5787.   file_close ();
  5788. }
  5789. #endif /* not atari */
  5790.  
  5791. /* Create the symbol table entries for `etext', `edata' and `end'.  */
  5792.  
  5793. void
  5794. symtab_init ()
  5795. {
  5796. #ifndef nounderscore
  5797.   edata_symbol = getsym ("_edata");
  5798.   etext_symbol = getsym ("_etext");
  5799.   end_symbol = getsym ("_end");
  5800. #else
  5801.   edata_symbol = getsym ("edata");
  5802.   etext_symbol = getsym ("etext");
  5803.   end_symbol = getsym ("end");
  5804. #endif
  5805.  
  5806.   edata_symbol->defined = N_DATA | N_EXT;
  5807.   etext_symbol->defined = N_TEXT | N_EXT;
  5808.   end_symbol->defined = N_BSS | N_EXT;
  5809.  
  5810.   edata_symbol->referenced = 1;
  5811.   etext_symbol->referenced = 1;
  5812.   end_symbol->referenced = 1;
  5813. }
  5814.  
  5815. /* Compute the hash code for symbol name KEY.  */
  5816.  
  5817. int
  5818. hash_string (key)
  5819.      char *key;
  5820. {
  5821.   register char *cp;
  5822.   register int k;
  5823.  
  5824.   cp = key;
  5825.   k = 0;
  5826.   while (*cp)
  5827.     k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
  5828.  
  5829.   return k;
  5830. }
  5831.  
  5832. /* Get the symbol table entry for the global symbol named KEY.
  5833.    Create one if there is none.  */
  5834.  
  5835. symbol *
  5836. getsym (key)
  5837.      char *key;
  5838. {
  5839.   register int hashval;
  5840.   register symbol *bp;
  5841.  
  5842.   /* Determine the proper bucket.  */
  5843.  
  5844.   hashval = hash_string (key) % TABSIZE;
  5845.  
  5846.   /* Search the bucket.  */
  5847.  
  5848.   for (bp = symtab[hashval]; bp; bp = bp->link)
  5849.     if (! strcmp (key, bp->name))
  5850.       return bp;
  5851.  
  5852.   /* Nothing was found; create a new symbol table entry.  */
  5853.  
  5854.   bp = (symbol *) xmalloc (sizeof (symbol));
  5855.   bp->refs = 0;
  5856.   bp->name = (char *) xmalloc (strlen (key) + 1);
  5857.   strcpy (bp->name, key);
  5858.   bp->defined = 0;
  5859.   bp->referenced = 0;
  5860.   bp->trace = 0;
  5861.   bp->value = 0;
  5862.   bp->max_common_size = 0;
  5863.   bp->warning = 0;
  5864.   bp->undef_refs = 0;
  5865.   bp->def_count = 0;
  5866.  
  5867.   /* Add the entry to the bucket.  */
  5868.  
  5869.   bp->link = symtab[hashval];
  5870.   symtab[hashval] = bp;
  5871.  
  5872.   ++num_hash_tab_syms;
  5873.  
  5874.   return bp;
  5875. }
  5876.  
  5877. /* Like `getsym' but return 0 if the symbol is not already known.  */
  5878.  
  5879. symbol *
  5880. getsym_soft (key)
  5881.      char *key;
  5882. {
  5883.   register int hashval;
  5884.   register symbol *bp;
  5885.  
  5886.   /* Determine which bucket.  */
  5887.  
  5888.   hashval = hash_string (key) % TABSIZE;
  5889.  
  5890.   /* Search the bucket.  */
  5891.  
  5892.   for (bp = symtab[hashval]; bp; bp = bp->link)
  5893.     if (! strcmp (key, bp->name))
  5894.       return bp;
  5895.  
  5896.   return 0;
  5897. }
  5898.  
  5899. /* Report a fatal error.
  5900.    STRING is a printf format string and ARG is one arg for it.  */
  5901.  
  5902. #if __STDC__
  5903. void
  5904. fatal (char *string, ...)
  5905. {
  5906.     va_list ap;
  5907.     
  5908.     va_start(ap, string);
  5909.     fprintf (stderr, "%s: ", progname);
  5910.     vfprintf (stderr, string, ap);
  5911.     fprintf (stderr, "\n");
  5912.     va_end(ap);
  5913.     exit (1);
  5914. }
  5915. #else
  5916. void
  5917. fatal (va_alist)
  5918. va_dcl
  5919. {
  5920.     va_list ap;
  5921.     char *string;
  5922.  
  5923.     va_start(ap);
  5924.     string = va_arg(ap, char *);
  5925.     fprintf (stderr, "%s: ", progname);
  5926.     vfprintf (stderr, string, ap);
  5927.     fprintf (stderr, "\n");
  5928.     va_end(ap);
  5929.     exit (1);
  5930. }
  5931. #endif
  5932.  
  5933.  
  5934. /* Report a fatal error.  The error message is STRING
  5935.    followed by the filename of ENTRY.  */
  5936.  
  5937. void
  5938. fatal_with_file (string, entry)
  5939.      char *string;
  5940.      struct file_entry *entry;
  5941. {
  5942.   fprintf (stderr, "ld: ");
  5943.   fprintf (stderr, string);
  5944.   print_file_name (entry, stderr);
  5945.   fprintf (stderr, "\n");
  5946.   exit (1);
  5947. }
  5948.  
  5949. /* Report a fatal error using the message for the last failed system call,
  5950.    followed by the string NAME.  */
  5951.  
  5952. void
  5953. perror_name (name)
  5954.      char *name;
  5955. {
  5956.   extern int errno, sys_nerr;
  5957.   extern char *sys_errlist[];
  5958.   char *s;
  5959.  
  5960.   if (errno < sys_nerr)
  5961.     s = concat ("", sys_errlist[errno], " for %s");
  5962.   else
  5963.     s = "cannot open %s";
  5964.   fatal (s, name);
  5965. }
  5966.  
  5967. /* Report a fatal error using the message for the last failed system call,
  5968.    followed by the name of file ENTRY.  */
  5969.  
  5970. void
  5971. perror_file (entry)
  5972.      struct file_entry *entry;
  5973. {
  5974.   extern int errno, sys_nerr;
  5975.   extern char *sys_errlist[];
  5976.   char *s;
  5977.  
  5978.   if (errno < sys_nerr)
  5979.     s = concat ("", sys_errlist[errno], " for ");
  5980.   else
  5981.     s = "cannot open ";
  5982.   fatal_with_file (s, entry);
  5983. }
  5984.  
  5985. /* Report a nonfatal error.
  5986.    STRING is a format for printf, and ARG1 ... ARG3 are args for it.  */
  5987.  
  5988. void
  5989. error (string, arg1, arg2, arg3)
  5990.      char *string, *arg1, *arg2, *arg3;
  5991. {
  5992.   fprintf (stderr, "%s: ", progname);
  5993.   fprintf (stderr, string, arg1, arg2, arg3);
  5994.   fprintf (stderr, "\n");
  5995. }
  5996.  
  5997.  
  5998. /* Output COUNT*ELTSIZE bytes of data at BUF
  5999.    to the descriptor DESC.  */
  6000.  
  6001. void
  6002. mywrite (buf, count, eltsize, desc)
  6003.      void *buf;
  6004.      int count;
  6005.      int eltsize;
  6006.      FILE *desc;
  6007. {
  6008.   if((eltsize != 0) && (count != 0))
  6009.   {
  6010.       if(count != fwrite(buf, eltsize, count, desc))
  6011.         perror_name(output_filename);
  6012.   }
  6013. }
  6014.  
  6015. #if (!(defined(CROSSATARI) || defined(atarist) || defined(atariminix)))
  6016. /* Output PADDING zero-bytes to descriptor OUTDESC.
  6017.    PADDING may be negative; in that case, do nothing.  */
  6018.  
  6019. void
  6020. padfile (padding, outdesc)
  6021.      int padding;
  6022.      FILE *outdesc;
  6023. {
  6024.   register char *buf;
  6025.   if (padding <= 0)
  6026.     return;
  6027.  
  6028.   buf = (char *) alloca (padding);
  6029.   bzero (buf, padding);
  6030.   mywrite (buf, padding, 1, outdesc);
  6031. }
  6032. #endif /* not atari */
  6033.  
  6034. /* Return a newly-allocated string
  6035.    whose contents concatenate the strings S1, S2, S3.  */
  6036.  
  6037. char *
  6038. concat (s1, s2, s3)
  6039.      char *s1, *s2, *s3;
  6040. {
  6041.   register int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  6042.   register char *result = (char *) xmalloc (len1 + len2 + len3 + 10);
  6043.  
  6044.   strcpy (result, s1);
  6045.   strcpy (result + len1, s2);
  6046.   strcpy (result + len1 + len2, s3);
  6047.   result[len1 + len2 + len3] = 0;
  6048.  
  6049.   return result;
  6050. }
  6051.  
  6052. /* Parse the string ARG using scanf format FORMAT, and return the result.
  6053.    If it does not parse, report fatal error
  6054.    generating the error message using format string ERROR and ARG as arg.  */
  6055.  
  6056. int
  6057. parse (arg, format, error)
  6058.      char *arg, *format, *error;
  6059. {
  6060.   int x;
  6061.   if (1 != sscanf (arg, format, &x))
  6062.     fatal (error, arg);
  6063.   return x;
  6064. }
  6065.  
  6066. /* Like malloc but get fatal error if memory is exhausted.  */
  6067.  
  6068. void *
  6069. xmalloc (size)
  6070.      int size;
  6071. {
  6072.   register void *result = malloc (size);
  6073.   if (!result)
  6074.     fatal ("virtual memory exhausted", 0);
  6075.   return result;
  6076. }
  6077.  
  6078. /* Like realloc but get fatal error if memory is exhausted.  */
  6079.  
  6080. void *
  6081. xrealloc (ptr, size)
  6082.      void *ptr;
  6083.      int size;
  6084. {
  6085.   register void *result = realloc (ptr, size);
  6086.   if (!result)
  6087.     fatal ("virtual memory exhausted", 0);
  6088.   return result;
  6089. }
  6090.  
  6091. #ifdef USG
  6092.  
  6093. void
  6094. bzero (p, n)
  6095.      char *p;
  6096. {
  6097.   memset (p, 0, n);
  6098. }
  6099.  
  6100. void
  6101. bcopy (from, to, n)
  6102.      char *from, *to;
  6103. {
  6104.   memcpy (to, from, n);
  6105. }
  6106.  
  6107. #ifndef pyr
  6108. getpagesize ()
  6109. {
  6110.   return (4096);
  6111. }
  6112. #endif
  6113.  
  6114. #endif
  6115.  
  6116. #if (defined(MINIX) || defined(atariminix))
  6117. /* Write the symbol table and string table into minix_out_file.  */
  6118.  
  6119. #ifdef SZ_HEAD
  6120. #undef SZ_HEAD        /* conflict with def in out.h */
  6121. #endif
  6122.  
  6123. #ifdef SF_HEAD
  6124. #undef SF_HEAD        /* conflict with def in out.h */
  6125. #endif
  6126.  
  6127. #include <out.h> /* format of output of /usr/lib/ld -- minixSt update#9 */
  6128.  
  6129. FILE *minix_outname_file, *minix_strings_file;
  6130. unsigned short minix_nname;
  6131. long minix_nchar;
  6132. unsigned short minix_swap_short();
  6133. long minix_swap_long();
  6134.  
  6135. write_minix_sym(p, str, gflag, fflag)
  6136. register struct nlist * p;
  6137. char * str;
  6138. int gflag, fflag;
  6139. {
  6140.   struct outname sym;
  6141.   int i;
  6142.  
  6143. /* fprintf(stderr, "sym %s\n", str); */
  6144.  
  6145.   sym.on_foff = minix_nchar;    /* we will patch in offset later */
  6146.   for(i = 0; str[i]; i++)
  6147.   {
  6148.       putc(str[i], minix_strings_file);
  6149.       minix_nchar++;
  6150.   }
  6151.   putc(0, minix_strings_file);
  6152.   minix_nchar++;
  6153.   
  6154.   switch (p->n_type & N_TYPE)
  6155.     {
  6156.     case N_UNDF: sym.on_type = S_TYP & S_UND; break;
  6157.     case N_ABS:  sym.on_type = S_TYP & S_ABS; break;
  6158.     case N_TEXT: sym.on_type = S_TYP & S_MIN; break;
  6159.     case N_DATA: sym.on_type = S_TYP & (S_MIN + 2); break;
  6160.     case N_BSS:  sym.on_type = S_TYP & (S_MIN + 3); break;
  6161.     default:     sym.on_type = 0;
  6162.       /* figure out rest of this later */
  6163.     }
  6164.   if(gflag == 1) 
  6165.       sym.on_type |= S_EXT; 
  6166.   if(fflag == 1)
  6167.       sym.on_type |= S_ETC & S_FIL;
  6168.   sym.on_type = minix_swap_short(&sym.on_type);
  6169.   sym.on_desc = 0;
  6170.   sym.on_valu = p->n_value;
  6171.   sym.on_valu = minix_swap_long(&sym.on_valu);
  6172. #ifndef WORD_ALIGNED
  6173.   fwrite(&sym, sizeof(sym), 1, minix_outname_file);
  6174. #else
  6175.   fwrite(&sym.on_foff, sizeof sym.on_foff, 1, minix_outname_file);
  6176.   fwrite(&sym.on_type, sizeof sym.on_type, 1, minix_outname_file);
  6177.   fwrite(&sym.on_desc, sizeof sym.on_desc, 1, minix_outname_file);
  6178.   fwrite(&sym.on_valu, sizeof sym.on_valu, 1, minix_outname_file);
  6179. #endif  
  6180.   minix_nname++;
  6181.   
  6182. }
  6183.  
  6184. void write_minix_syms(entry, syms_written_addr)
  6185. struct file_entry * entry;
  6186. int * syms_written_addr;
  6187. {
  6188.   register struct nlist *p = entry->symbols;
  6189.   register struct nlist *end = p + entry->header.a_syms / sizeof (struct nlist);
  6190.   int max_syms = (entry->header.a_syms / sizeof (struct nlist)) + 1;
  6191.  
  6192.   /* Read the file's string table.  */
  6193.  
  6194.   entry->strings = (char *) xmalloc (entry->string_size);
  6195.   read_entry_strings (file_open (entry), entry);
  6196.  
  6197.   /* Generate a local symbol for the start of this file's text.  */
  6198.  
  6199.     {
  6200.       struct nlist nl;
  6201.  
  6202.       nl.n_type = N_TEXT;
  6203. /*      nl.n_un.n_strx = assign_string_table_index (entry->local_sym_name); */
  6204.       nl.n_value = entry->text_start_address;
  6205.       nl.n_desc = 0;
  6206.       nl.n_other = 0;
  6207. /*      *bufp++ = nl;    */
  6208.       write_minix_sym(&nl, entry->local_sym_name, 0, 1);
  6209.       (*syms_written_addr)++;
  6210. /* necessary? */
  6211.       entry->local_syms_offset = *syms_written_addr * sizeof (struct nlist);
  6212.     }
  6213.  
  6214.   for (; p < end; p++)
  6215.     {
  6216.       register int type = p->n_type;
  6217.       register int write = 0;
  6218.  
  6219.       /* WRITE gets 1 for a non-global symbol that should be written.  */
  6220.  
  6221.       if (!(type & (N_STAB | N_EXT)))
  6222.         /* ordinary local symbol */
  6223.     write = !((p->n_un.n_strx + entry->strings)[0] == 'L');
  6224.       else if (!(type & N_EXT))
  6225.     /* debugger symbol */
  6226.     write = 0;
  6227.  
  6228.       if (write)
  6229.     {
  6230.       /* If this symbol has a name, write it */
  6231.  
  6232.       if (p->n_un.n_strx)
  6233.         {
  6234.         write_minix_sym(p, p->n_un.n_strx + entry->strings, 0, 0);
  6235.         (*syms_written_addr)++;
  6236.         }
  6237.     }
  6238.     }
  6239.   free (entry->strings);
  6240.   entry->strings = NULL;  
  6241. }
  6242.  
  6243. void do_write_minix_out ()
  6244. {
  6245.   int n_syms_written = 0;
  6246.   register symbol *sp;
  6247.   int i;
  6248.   char minix_outname_name[10], minix_strings_name[10];
  6249.   struct outhead oh;
  6250.   struct outname sym;
  6251.   long string_off;
  6252.     
  6253.   /* create two tmp files, one for outname structs one for strings, */
  6254.   /* later these two are concatenated into minix_out_file and unlinked */
  6255.   strcpy(minix_outname_name, "ldnXXXXXX");
  6256.   strcpy(minix_strings_name, "ldsXXXXXX");
  6257.   mktemp(minix_outname_name);
  6258.   mktemp(minix_strings_name);
  6259.   if(((minix_outname_file = fopen(minix_outname_name, "w"))
  6260.       == (FILE *)NULL) ||
  6261.      ((minix_strings_file = fopen(minix_strings_name, "w"))
  6262.       == (FILE *)NULL))
  6263.       fatal("Cannot creat tmp files", (char *)NULL);
  6264.   /* make up a out.h format header */
  6265.   oh.oh_magic = O_MAGIC;
  6266.   oh.oh_magic = minix_swap_short(&oh.oh_magic);
  6267.   oh.oh_stamp = O_STAMP;
  6268.   oh.oh_stamp = minix_swap_short(&oh.oh_stamp);
  6269.   oh.oh_flags = 0;
  6270.   oh.oh_nsect = 0;
  6271.   oh.oh_nrelo = 0;
  6272.   oh.oh_nname = 0;    /* will be patched */
  6273.   oh.oh_nemit = 0;
  6274.   oh.oh_nchar = 0;    /* will be patched */
  6275. #ifndef WORD_ALIGNED
  6276.   fwrite(&oh, sizeof(oh), 1, minix_outname_file);
  6277. #else
  6278.   fwrite(&oh.oh_magic, sizeof oh.oh_magic, 1, minix_outname_file);
  6279.   fwrite(&oh.oh_stamp, sizeof oh.oh_stamp, 1, minix_outname_file);
  6280.   fwrite(&oh.oh_flags, sizeof oh.oh_flags, 1, minix_outname_file);
  6281.   fwrite(&oh.oh_nsect, sizeof oh.oh_nsect, 1, minix_outname_file);
  6282.   fwrite(&oh.oh_nrelo, sizeof oh.oh_nrelo, 1, minix_outname_file);
  6283.   fwrite(&oh.oh_nname, sizeof oh.oh_nname, 1, minix_outname_file);
  6284.   fwrite(&oh.oh_nemit, sizeof oh.oh_nemit, 1, minix_outname_file);
  6285.   fwrite(&oh.oh_nchar, sizeof oh.oh_nchar, 1, minix_outname_file);
  6286. #endif
  6287.  
  6288.   minix_nname = 0;
  6289.   minix_nchar = 0L;
  6290.   
  6291.   each_file(write_minix_syms, &n_syms_written);
  6292.  
  6293.   /* Now write out the global symbols.  */
  6294.  
  6295.   /* Scan the symbol hash table, bucket by bucket.  */
  6296.  
  6297.   for (i = 0; i < TABSIZE; i++)
  6298.     for (sp = symtab[i]; sp; sp = sp->link)
  6299.       {
  6300.     struct nlist nl;
  6301.  
  6302.     nl.n_other = 0;
  6303.     nl.n_desc = 0;
  6304.  
  6305.     /* Compute a `struct nlist' for the symbol.  */
  6306.  
  6307.     if (sp->defined || sp->referenced)
  6308.       {
  6309.         if (!sp->defined)          /* undefined -- legit only if -r */
  6310.           {
  6311.         nl.n_type = N_UNDF | N_EXT;
  6312.         nl.n_value = 0;
  6313.           }
  6314.         else if (sp->defined > 1) /* defined with known type */
  6315.           {
  6316.         nl.n_type = sp->defined;
  6317.         nl.n_value = sp->value;
  6318.           }
  6319.         else if (sp->max_common_size) /* defined as common but not allocated. */
  6320.           {
  6321.         /* happens only with -r and not -d */
  6322.         /* write out a common definition */
  6323.         nl.n_type = N_UNDF | N_EXT;
  6324.         nl.n_value = sp->max_common_size;
  6325.           }
  6326.         else
  6327.           fatal ("internal error: %s defined in mysterious way", sp->name);
  6328.  
  6329.         /* write and count it.  */
  6330.  
  6331.         write_minix_sym(&nl, sp->name, 1, 0);
  6332.         n_syms_written++;
  6333.       }
  6334.       }
  6335. #if 0
  6336.   if (n_syms_written != nsyms)
  6337.     fprintf(stderr, "Bogon alert!  wrote %d syms, expected to write %d\n",
  6338.              n_syms_written, nsyms);
  6339. #endif
  6340.  
  6341.   /* close the temp files, open minix_out_file, and concat */
  6342.   fclose(minix_strings_file);
  6343. #ifndef WORD_ALIGNED
  6344.   string_off = minix_nname * sizeof(sym) + sizeof(oh);
  6345. #else
  6346.   string_off = minix_nname * 12 + 20;
  6347. #endif
  6348.   
  6349.   /* patch in oh_nname and oh_nchar */
  6350.   fseek(minix_outname_file, 10L, 0);
  6351.   minix_nname = minix_swap_short(&minix_nname);
  6352.   fwrite(&minix_nname, sizeof(ushort), 1, minix_outname_file);
  6353.   fseek(minix_outname_file, 16L, 0);
  6354.   minix_nchar = minix_swap_long(&minix_nchar);
  6355.   fwrite(&minix_nchar, sizeof(long), 1, minix_outname_file);
  6356.   
  6357.   fclose(minix_outname_file);
  6358.  
  6359.   if(((minix_outname_file = fopen(minix_outname_name, "r"))
  6360.       == (FILE *)NULL) ||
  6361.      ((minix_strings_file = fopen(minix_strings_name, "r"))
  6362.       == (FILE *)NULL))
  6363.       fatal("Cannot open tmp files for read", (char *)NULL);
  6364.   if((minix_out_filep = fopen(minix_out_filename, "w")) == (FILE *)NULL)
  6365.       fatal("Cannot create %s", minix_out_filename);
  6366.  
  6367. #ifndef WORD_ALIGNED
  6368.   fread(&oh, sizeof(oh), 1, minix_outname_file);
  6369.   fwrite(&oh, sizeof(oh), 1, minix_out_filep);
  6370.   while(fread(&sym, sizeof(sym), 1, minix_outname_file) == 1)
  6371.   {
  6372.       sym.on_foff += string_off;
  6373.       sym.on_foff = minix_swap_long(&sym.on_foff);
  6374.       fwrite(&sym, sizeof(sym), 1, minix_out_filep);
  6375.   }
  6376. #else
  6377.   fread(&oh, 20, 1, minix_outname_file);
  6378.   fwrite(&oh, 20, 1, minix_out_filep);
  6379.   while(fread(&sym.on_foff, sizeof sym.on_foff, 1, minix_outname_file) == 1)
  6380.   {
  6381.       fread(&sym.on_type, sizeof sym.on_type, 1, minix_outname_file);
  6382.       fread(&sym.on_desc, sizeof sym.on_desc, 1, minix_outname_file);
  6383.       fread(&sym.on_valu, sizeof sym.on_valu, 1, minix_outname_file);
  6384.       
  6385.       sym.on_foff += string_off;
  6386.       sym.on_foff = minix_swap_long(&sym.on_foff);
  6387.  
  6388.       fwrite(&sym.on_foff, sizeof sym.on_foff, 1, minix_out_filep);
  6389.       fwrite(&sym.on_type, sizeof sym.on_type, 1, minix_out_filep);
  6390.       fwrite(&sym.on_desc, sizeof sym.on_desc, 1, minix_out_filep);
  6391.       fwrite(&sym.on_valu, sizeof sym.on_valu, 1, minix_out_filep);
  6392.  
  6393.   }
  6394. #endif
  6395.  
  6396. #if 0
  6397.   fprintf(stderr,"Curr %ld Str_off %ld\n", ftell(minix_out_filep), string_off);
  6398. #endif
  6399.   
  6400.   while((i = getc(minix_strings_file)) != EOF)
  6401.       putc(i, minix_out_filep);
  6402.   fclose(minix_strings_file);
  6403.   fclose(minix_outname_file);
  6404.   fclose(minix_out_filep);
  6405.  
  6406.   unlink(minix_strings_name);
  6407.   unlink(minix_outname_name);
  6408.   
  6409. }
  6410.  
  6411. unsigned short minix_swap_short(s)
  6412. unsigned char s[];
  6413. {
  6414.     unsigned short i = (s[1] << 8) | s[0];
  6415.     
  6416.     return i;
  6417. }
  6418.  
  6419. long minix_swap_long(s)
  6420. unsigned char s[];
  6421. {
  6422.     unsigned long i = (s[3] << 24) | (s[2] << 16) | (s[1] << 8) | s[0];
  6423.     return i;
  6424. }
  6425.  
  6426. #endif
  6427.